Springboot使用thymeleaf进行页面跳转
目前Java Web开发推荐使用模板引擎,不建议使用JSP页面
JSP缺点:本质上就是Servlet,需要后台编译,耗时,效率低
模板引擎:不需要编译,速度快
常见的模板引擎:Freemarker、Velocity、Thymeleaf等
SpringBoot推荐使用Thymeleaf(C母赖夫),且默认不支持JSP,因为JSP必须要打包war包才行
现阶段项目中更多使用MVVM框架,前后段分离如:Vue.js、Angular、React。
添加thymeleaf模板引擎需要修改两处文件(其实添加一个pom依赖就ok)。
1.pom文件, <!-- 引入 thymeleaf 模板依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2..application.properties文件,新增下面配置。
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
3.java代码 如下方式,templates目录下的HTML页面默认不能被直接访问,需要通过controller来访问,由thymeleaf来渲染
@ControllerAdvice
@RequestMapping("index")
public class SpringBootController {@RequestMapping("/thymeleaf")public String thymeleaf(Model model) {model.addAttribute("name", "qushen");System.out.println("从Controller跳转thymeleaf");return "user/thymeleaf";}
}
HTML写法如下:具体Thymeleaf写法可以参考官网:https://www.thymeleaf.org/documentation.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><div>hello Spring boot!</div><p th:text="${name}">Welcome to our grocery store!</p></body>
</html>s
显示效果如下:
Springboot使用thymeleaf进行页面跳转相关推荐
- aspx跳转页面的符号_解决SpringBoot+MyBatis框架下页面跳转问题与代码优化处理。...
一.关于页面跳转的疑问 后台「return "/pool/index"」返回的是字符串,会自动寻找名为"index"的页面模板文件是怎样实现的? 答:这是Spr ...
- springboot的thymeleaf一个页面中引入其它页面
引入页面内容(copy为自定义的) <div th:fragment="copy">这是引入内容</div> 引入方式(footer为引入页面名称) < ...
- SpringBoot 整合模板引擎 Thymeleaf 页面跳转失败的解决方案
1. 出错现象 我们知道 Thymeleaf 模板引擎规定的文件路劲应该是在 templates 目录下面的. 但是笔者在开发的过程中,满足了上述要求但是还是没能映射成功. 我们来看看 index.h ...
- SpringBoot页面跳转访问css、js等静态资源引用无效解决
SpringBoot页面跳转访问css.js等静态资源引用无效解决 原文链接:https://blog.csdn.net/qq_41647999/article/details/83788265 目录 ...
- thymeleaf 使用页面报错_异常处理-SpringBoot中thymeleaf对应前台页面大于号\小于号使用问题...
浏览器报错信息: There was an unexpected error (type=Internal Server Error, status=500). Exception parsing d ...
- Springboot+JdbcTemplate +thymeleaf 页面 做迷你版的bug系统
https://www.cnblogs.com/qianjinyan/p/10065160.html 在我上一篇随笔中介绍了关于要做的系统的数据结构,连接如上 今天实现连接mssql server, ...
- springboot 直接转发调用_springboot-过滤器的页面跳转【重定向与请求转发】-异常报错...
springboot中,增加过滤器后,页面重定向跳转与请求转发跳转,都出现报错. 求大佬帮忙解答,谢谢! 一.页面跳转代码: 1.重定向代码: response.sendRedirect(" ...
- springboot使用thymeleaf完成数据的页面展示
上一篇介绍了使用jsp完成数据的页面展示 ,但是springboot并不推荐使用jsp,会产生很多问题.官方推荐使用thymeleaf,这里我们将上一篇的jsp页面展示修改为使用thymeleaf,通 ...
- thymeleaf 使用页面报错_SpringBoot 使用thymeleaf 跳转页面时,总是提示404找不到页面...
Thymeleaf的使用和版本关系很大,如果路径配置正确却一直无法正常返回,可能就是版本错误导致. 想构建简单页面跳转,正常跳转index.html,404和500跳转到专门的页面,配置正确,路径也正 ...
最新文章
- 几行代码构建全功能的对象检测模型,他是如何做到的?
- 如何让一个应届毕业生快速成长?
- 在python中,小数点前的0可以省略不写...
- 超大规模预训练模型专场直播:模型真的越大越好吗?
- python分词和词频统计
- w10自动删除文件怎么关了_回收站删除的文件怎么恢复?
- eclipse中tomcat能正常启动,但是访问不了tomcat首页(问题解决)
- 计算机论文答辩2分钟演讲稿,论文答辩演讲稿
- oracle导出表904错误,月光软件站 - 编程文档 - 数据库 - EXP-00008: ORACLE error 904 encountered的解决方法...
- 跨平台flutter- window与Android Studio环境配置
- oracle归档日志百分比,oracle归档日志过满清理
- js find(),findIndex()方法的使用
- 带电插拔损坏设备原理_那些设备可以热插拔?
- SAP S4 FI后台详细配置教程- PART5 (应收帐目和应付帐目配置篇)
- 免费快递查询_Api接口
- 桃词典 Peach Dictionary 简易英语词典app开发 安卓软件开发 The End 导航页及收尾工作
- 中国海洋大学各个校区都有什么专业
- office2010
- 小李的电商日记 22.10.12
- python 货币格式_Python中的货币格式
热门文章
- docker安装zookeeper(单节点安装)
- gson格式化参数 对象转Map
- Android 实现九宫格、点击图片放大全屏浏览等
- Java中UDP协议的基本原理和简单用法
- Linux技巧:自动挂载UDF光盘的技巧
- 清除WINDOW远程登录服务器的信息
- Android解决ScrollView中的滑动效果导致GestureDetector中的OnFling不能正常工作问题
- Spring 使用context:annotation-config的设置
- Cheatsheet: 2011 12.01 ~ 12.12
- C语言2011计算机二级c语言考点:c语言的基础知识