Spring boot 梳理 - 模版引擎 -freemarker

  1. 开发环境中关闭缓存
    1. spring:
          thymeleaf:
              cache: false
          freemarker:
              cache: false
  2. Spring boot 集成 freemarker
    1.         <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-freemarker</artifactId>
              </dependency>

       

    2. oa/src/main/resources/templates/hello.ftl
      1. <!DOCTYPE html>  
        <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  
              xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">  
            <head>  
                <title>Hello World!</title>  
            </head>  
            <body>
               <center>
               OK
               </center>
            </body>  
        </html>

         

    3. @Controller
      1. @Controller
        @RequestMapping("/fruit")
        public class FruitController {
            @RequestMapping(value="{fruitName}", method = RequestMethod.GET)
            public String getFruit(@PathVariable String fruitName, ModelMap model) {
        
                //Fruit fruit = new Fruit(fruitName, 1000);
                HashMap<String,String> fruit=new HashMap<String,String>();
                fruit.put("fruit", "fruit");
                model.addAttribute("model", fruit);
                
                return "hello";
        
            }
        }

         

    4. 访问
      1. http://localhost:8080/fruit/a
      2. 显示 OK
    5. 可以设置freemarker属性
      1. application.properties
      2. spring.freemarker.allow-request-override=false
        spring.freemarker.cache=false
        spring.freemarker.check-template-location=true
        spring.freemarker.charset=UTF-8
        spring.freemarker.content-type=text/html
        spring.freemarker.expose-request-attributes=false
        spring.freemarker.expose-session-attributes=false
        spring.freemarker.expose-spring-macro-helpers=false

         

posted on 2019-01-01 22:03  手握太阳  阅读(153)  评论(0编辑  收藏  举报

导航