SpringBoot学习-支持thymeleaf模板引擎

来源:互联网 发布:rends a10 2代编程 编辑:程序博客网 时间:2024/05/17 15:38

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=falsespring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html
基本上就可以了,现在写个controller测试一下

@Controllerpublic 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标签内是没有收尾的,会引起报错,我们加上收尾的“/”就行.