spring boot 使用thymeleaf实现jsp的WEB-iNF安全目录
spring boot 2 默认模板引擎是thymeleaf,而可以使用的有freemarker,Velocity,JSP(需要创建webapp目录并引入相关依赖)
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
进行配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false>
在static下创建index.html
在templates下创建其他页面,则templates下html文件无法直径访问
写视图控制器
第一种 使用@Controller直接返回页面名的字符串
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/showHello" )
public Strign showHello() {
return "hello";
}
}
第二种 使用@RestController 则只能使用ModelAndView返回视图名(因为RestController 会将视图名当作字符串返回)
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/showHello" )
public ModelAndView showHello() {
User user = userDao.getUserByUsername("test");
return new ModelAndView("hello");
}
}