Thymeleaf的学习

1.引入依赖

maven中直接引入html

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

能够查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,因此能够把spring-boot-starter-web的依赖去掉.java

2.配置视图解析器

spring-boot不少配置都有默认配置,好比默认页面映射路径为 
classpath:/templates/*.html 
一样静态文件路径为 
classpath:/static/web

在application.properties中能够配置thymeleaf模板解析器属性.就像使用springMVC的JSP解析器配置同样spring

#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,否则无法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end

具体能够配置的参数能够查看 
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值.缓存

3.编写DEMO

1.控制器app

@Controller
    public class HelloController {

        private Logger logger = LoggerFactory.getLogger(HelloController.class);
        /**
         * 测试hello
         * @return
         */
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String hello(Model model) {
            model.addAttribute("name", "Dear");
            return "hello";
        }

    }

2.view(注释为IDEA生成的索引,便于IDEA补全)maven

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<!--/*@thymesVar id="name" type="java.lang.String"*/-->
<p th:text="'Hello!, ' + ${name} + '!'" >3333</p>
</body>
</html>
相关文章
相关标签/搜索