zoukankan      html  css  js  c++  java
  • SpringBoot

    案例一

     1、导入依赖

    </properties>
        <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    
    </parent>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    <dependency> <!-- spring-boot-starter-web是为我们提供了包括mvc,aop等需要的一些jar --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 因为我们已经配置了 parent 中的version 所以这里不需要指定version了 --> </dependency>

      2、创建FirstController类

    @RestController
    public class FirstController {
    
    
        @RequestMapping("/firstController")
        public String firstController(){
            System.out.println("第一个请求");
            return "Hello SpringBoot";
        }
    }

    3创建StartSpringBoot 测试类

    @SpringBootApplication
    public class StartSpringBoot {
        public static void main(String[] args) {
            SpringApplication.run(StartSpringBoot.class,args);
        }
    }

    网页访问

     

    二、SpringBoot静态资源访问

      1、在resources文件下创建static包,再static下创建img包

      

      

         三、SpringBoot解决异常问题

      1、在Controller类中定义一个异常

        

    @RestController
    public class FirstController {
    
    
        @RequestMapping("/firstController")
        public String firstController(){
            int result=5/0;
            System.out.println("第一个请求");
            return "Hello SpringBoot";
        }
    }

    2. 定义异常类

    @ControllerAdvice
    public class ExceptionHan {
        @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public Map<String,Object> exceHandler(){
            Map<String,Object> map=new HashMap<>();
            map.put("error","500");
            map.put("msg","异常");
            return map;
    
        }

      3、结果如下

        

    四、SpringBoot使用freemarker

      1、导入依赖freemarker

     <dependency>
          <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-freemarker</artifactId>
     </dependency>

      2、配置application.propertiesleuk文件

    ## Freemarker 配置
    spring.freemarker.template-loader-path=classpath:/templates/
    spring.freemarker.cache=false
    spring.freemarker.charset=UTF-8
    spring.freemarker.check-template-location=true
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.request-context-attribute=request
    spring.freemarker.prefix=/
    spring.freemarker.suffix=.ftl

      3、在resources包下创建templates包并创建holle.ftl模板

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>SpringBoot整合FreeMarker</title>
    </head>
    <body>
        欢迎:
      ` ${name}
    
        <#list stulist as stu>
            ${stu.stu_name}
        </#list>
    
        <#if 1==1>
            等等等等!!!
        </#if>
    
        <#list userList as user>
            ${user}
        </#list>
    </body>
    </html>

      4、创建StartSpringBoot启动类

    package com.freemarker;
    
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class StartSpringBoot {
        public static void main(String[] args){
            SpringApplication.run(StartSpringBoot.class,args);
        }
    }

      5、创建Controller类freemarkerController类

    package com.freemarker.Controller;
    
    import com.freemarker.entity.student;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    @RequestMapping("/free")
    public class freemarkerController {
        @RequestMapping("/freeFirst")
        public String freeFirst(ModelMap map){
            map.put("name","张三");
            return "hello"; //找templates/name.ftl
        }
    
        @RequestMapping("/freeSecond")
        public String freeSecond(ModelMap map){
            List<String> list=new ArrayList<>();
            list.add("张三");
            list.add("李四");
            list.add("王五");
            map.put("userList",list);
            return "hello";
        }
    
        @RequestMapping("/freeThread")
        public String freeThread(ModelMap map){
            List<student> list=new ArrayList<>();
            student stu=new student();
            stu.setStu_id(1);
            stu.setStu_name("小王");
            list.add(stu);
            map.put("stulist",list);
            return "hello";
        }
    }

      6、页面访问实现效果如下

        

  • 相关阅读:
    一本通1559跳跳棋
    一本通1558聚会
    一本通1555【例 4】次小生成树
    P1880 [NOI1995]石子合并
    P2066 机器分配
    P2073 送花
    P1886 滑动窗口
    P1637 三元上升子序列
    P1533 可怜的狗狗
    P1631 序列合并
  • 原文地址:https://www.cnblogs.com/Chencheno/p/12017784.html
Copyright © 2011-2022 走看看