模板引擎有:JSP、Velocity、Freemarker、Thymeleaf 等.....
对于模板引擎,SpringBoot(springboot使用的是嵌入式的tomcat所以不支持jsp)推荐的Thymeleaf;
1)、引入thymeleaf (thyme:百里香,leaf:叶子 thymeleaf:百里香叶)
pom文件中导入依赖坐标(即开启springboot中的thymeleaf启动器):
org.springframework.boot
spring-boot-starter-thymeleaf
切换其版本操作:在pom文件中的的properties标签中指定版本
3.0.9.RELEASE
2.2.2
2)、thymeleaf的使用
使用规则:
ThymeleafProperties.java
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
测试:
@Controller
public class TestController {
@RequestMapping("/test")
public String testThymelea(){
//classpath:/templates/th.html
return "th";
}
}
静态文件
测试结果:
3)、使用案例:
编写一个mapper设置hello的值为你好;然后跳转到success页面
@RequestMapping("/success")public String success(Mapmap){//使用thymeleaf模板引擎,自动找thymeleaf会找template下的页面进行渲染,
map.put("hello","你好!");return "success";
}
success.html;在标签中导入thymeleaf的命名空间,有提示作用
成功