您的位置:首页 > 编程语言 > Java开发

SpringBoot学习-支持thymeleaf模板引擎

2017-09-19 16:10 483 查看
Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎。配置过程比较简单,引入jar支持就行

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>application.properties文件配置一些信息(非必须)
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html基本上就可以了,现在写个controller测试一下
@Controller
public class Test2Controller {

@RequestMapping("to")
public String to(){
return "hello";
}

@RequestMapping("test")
public String test(){
return "test";
}

}注意一点,controller的注解是@Controller,而不是@RestController,区别在项目搭建的时候说过



在templates目录下新建几个页面,SpringBoot有个默认首页的设置,可以把index.html放在static静态资源目录下,就可以直接访问了

注意:HTML中引用Js和CSS的文件需要也放在static目录下才能被正确访问

测试结果:



有一个小问题:

IDEA创建的HTML文件默认的meta标签内是没有收尾的,会引起报错,我们加上收尾的“/”就行.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thymeleaf