zoukankan      html  css  js  c++  java
  • SpringBoot基础及FreeMarker模板

    案例springboot_freemarker

     application.properties配置文件

    ###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

    文件夹中创建HelloFreeMarker.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>
    </body>
    </html>

    FreeController

    @Controller
    @RequestMapping("/free")
    public class FreeController {
    
        @RequestMapping("/freeFirst")
        public String freeFirst(ModelMap map){
            map.put("name","没穿裤子");
        }
    
        @RequestMapping("/freeSecond")
        public String freeSecond(ModelMap map){
            List<String> list=new ArrayList<>();
            list.add("张三");
            list.add("李四");
            list.add("王五");
            map.put("userList",list);
            return "helloFreeMarker";
        }
    
        @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 "helloFreeMarker";
        }
    }

    Student实体类

    public class Student {
        private Integer stu_id;
        private String stu_name;
    
        public Integer getStu_id() {
            return stu_id;
        }
    
        public void setStu_id(Integer stu_id) {
            this.stu_id = stu_id;
        }
    
        public String getStu_name() {
            return stu_name;
        }
    
        public void setStu_name(String stu_name) {
            this.stu_name = stu_name;
        }
    }

    案例springboot_hellow

    FirstController

    @RestController
    /**
     * 如果说在Controller类上加RestController注解代表该controller当中的所有方法都返回Json串
     */
    @RequestMapping("/first")
    public class FirstController {
        @RequestMapping("/firstRequest")
        public String firstRequest(){
            int result=5/0;
            System.out.println("第一个请求到达Controller");
            return "Hello SpringBoot";
        }
    }

    MyExceptionHandler

    @ControllerAdvice
    public class MyExceptionHandler {
        //捕获运行时异常
        @ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public Map<String,Object> exceHandler(){
            Map<String,Object> map=new HashMap<>();
            map.put("error","500");
            map.put("msg","您好,服务器暂时出现异常,请稍后重试");
            return map;
        }
    

    StartSpringBoot

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

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        静态资源HTML页面
    </body>
    </html>
  • 相关阅读:
    51 nod 1109 01组成的N的倍数
    zoj 1530 Find The Multiple
    洛谷 P1124 文件压缩
    洛谷 P1270 “访问”美术馆(树形DP)
    洛谷 P1272 重建道路(树形DP)
    ♫【CSS】命名颜色
    【注释】
    -_-#【命名】BEM
    ☀【jQuery插件】DOM 延迟渲染
    ☀【组件】getRequest
  • 原文地址:https://www.cnblogs.com/dabrk/p/12017105.html
Copyright © 2011-2022 走看看