springboot+thymeleaf(2)

操作步骤

(1)在pom.xml中引入thymeleaf;

(2)如何关闭thymeleaf缓存

(3)编写模板文件.html

(4)编写访问模板文件controller

1、在pom.xml中引入thymeleaf

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

2、如何关闭thymeleaf缓存

#在propertis配置文件中将spring.thymelef.cache=false
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html 
# set to false for hot refresh
#在propertis配置文件中将spring.thymelef.cache=false
#开发过程建议关闭缓存.
spring.thymeleaf.cache
=false

其中propertiesspring.thymeleaf.cache=false是关闭thymeleaf的缓存,不然在开发过程中修改页面不会立刻生效需要重启,生产可配置为true。

在项目resources目录下会有两个文件夹:static目录用于放置网站的静态内容如css、js、图片;templates目录用于放置项目使用的页面模板。

3|、编写thymeleaf模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
     <h1>
         Hello,thymeleaf
         <br />
         This is my first thymeleaf demo.
         
         
         <hr />
         
         welcome <span th:text="${name}"></span>
         
     </h1>
</body>
</html>

4、编写controller

/**
 * 注意:
 * 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
 * 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
 * 
 * 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
 * 
 * 
 * @author Angel -- 守护天使
 * @version v.0.1
 * @date 2016年12月24日
 */
@Controller
@RequestMapping("/templates")
public class TemplatesController {
    
    /**
     * 映射地址是:/templates/hello
     * @return
     */
    @RequestMapping("/hello")
    public String hello(Map<String,Object> map){
        //返回的是ModelAndView对象;
//        ModelAndView mv = new ModelAndView("hello");
//        return mv;
        map.put("name","Andy");
        return "hello";
    }
}
原文地址:https://www.cnblogs.com/a8457013/p/8065806.html