spring boot 2 快速开发-模板引擎

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");
	 }
}
默认访问index.html页面

在这里插入图片描述

点击登陆后跳转到hello.html 而无法直接访问

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值