15.全网最详教程-springboot 中如何集成jsp模板、如何集成thymeleaf 模板
一、集成jsp模板
1.集成jsp模板
1.1引入jsp的集成jar包
<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId>
</dependency>
1.2引入jsp运行插件
<build><finalName>springboot_day1</finalName><!--引入jsp运行插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
1.3 配置视图解析器
#在配置文件中引入视图解析器
spring:mvc:view:prefix: / # /代表访问项目中webapp中页面suffix: .jsp
1.4 第一种方式使用插件启动
1.5 第二种方式使用idea中指定工作目录启动 [推荐]
1.6 启动访问jsp页面
http://localhost:8989/cmfz/index.jsp
1.7 修改jsp无须重启应用
server.servlet.jsp.init-parameters.development=true
2.集成thymelaf模板
2.1 引入依赖
<!--使用thymelaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2.2 编写配置
spring.thymeleaf.prefix=classpath:/templates/ #使用模板目录
spring.thymeleaf.suffix=.html #使用模板后缀
spring.thymeleaf.encoding=UTF-8 #使用模板编码
spring.thymeleaf.enabled=true #开始thymelaf模板
spring.thymeleaf.servlet.content-type=text/html #使用模板响应类型
2.3 编写控制器测试
@Controller //一定要是@Controller 不能再使用@RestController注解
@RequestMapping("hello")
public class HelloController {@GetMapping("hello")public String hello(){System.out.println("测试与 thymeleaf 的集成");return "index";}
}
2.4 在templates目录中定义模板
2.5 测试访问
http://localhost:8989/springboot_day3/hello/hello
2.6 查看结果
2.7 开启直接访问html页面
spring.resources.static-locations=classpath:/templates/,classpath:/static/
2.8 测试结果
http://localhost:8989/springboot_day3/index.html
3.thymelaf基本使用
使用时必须在页面中加入thymeleaf如下命名空间:`
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3.1 展示单个数据
a. 设置数据
model.addAttribute("name","张三"); 或 request.setAttribute("name","小黑");
b. 获取数据
<span th:text="${name}"/> --->获取数据
注意:在springboot2.x版本中必须加入国际化配置文件才能正常展示:`
c. 获取并解析含有html标签数据
model.addAttribute("name","<a href=''>张三</a>");
model.addAttribute("username","小陈");
- 直接获取原样输出
<span th:text="${name}"/>
- 获取并解析
<span th:utext="${name}"/>
- 将数据赋值给表单元素
<input type="text" th:value="${username}"/>
# 总结1.使用 th:text="${属性名}" 获取对应数据,获取数据时会将对应标签中数据清空,因此最好是空标签2.使用 th:utext="${属性名}" 获取对应的数据,可以将数据中html先解析在渲染到页面3.使用 th:value="${属性名}" 获取数据直接作为表单元素value属性
3.2 展示对象数据
model.addAttribute("user",new User("21","xiaochen",23,new Date()));
id:<span th:text="${user.id}"></span>
name:<span th:text="${user.name}"></span>
age:<span th:text="${user.age}"></span>
bir: <span th:text="${user.bir}"></span> ==== <span th:text="${#dates.format(user.bir, 'yyyy-MM-dd HH:mm')}"></span> 日期格式化
3.3 条件展示数据
model.addAttribute("user",new User("21","xiaochen",23,new Date()));
<span th:if="${user.age} eq 23">青年
</span>
# 运算符gt:great than(大于)>ge:great equal(大于等于)>=eq:equal(等于)==lt:less than(小于)<le:less equal(小于等于)<=ne:not equal(不等于)!=
3.4 展示多条数据
- 直接遍历集合
<ul th:each="user:${users}"><li th:text="${user.id}"></li><li th:text="${user.name}"></li><li th:text="${user.age}"></li><li th:text="${#dates.format(user.bir,'yyyy-MM-dd')}"></li>
</ul>
- 遍历时获取遍历状态
<ul th:each="user,userStat:${users}"><li><span th:text="${userStat.count}"/>-<span th:text="${user.id}"/></li> 获取遍历次数 count 从1开始 index 从0开始<li><span th:text="${userStat.odd}"/>-<span th:text="${user.name}"/></li> 获取当前遍历是否是奇数行<li><span th:text="${userStat.even}"/>-<span th:text="${user.age}"/></li> 获取当前遍历是否是偶数行<li><span th:text="${userStat.size}"/>-<span th:text="${user.bir}"/></li> 获取当前集合的总条数
</ul>
3.5 引入静态资源
使用thymeleaf模板项目中静态资源默认放在resources路径小static目录中
- 项目中放入对应静态资源
- 页面中引入
<link rel="stylesheet" th:href="@{/css/index.css}"><script th:src="@{/js/jquery-min.js}"></script>
15.全网最详教程-springboot 中如何集成jsp模板、如何集成thymeleaf 模板相关推荐
- 如何让springboot中的某些html文件不经过thymeleaf模板解析?
这个thymeleaf有时有用,有时用不着. 但默认设置,所有的html都会经过它解析. 我的作法,是新建public,在resource里,所有css,js所放里面.(当然,static下也是OK的 ...
- SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎
SpringBoot 学习笔记 Part12 1. thymeleaf简介 SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染. Thymeleaf is a modern ...
- java分布式会话redis_详解springboot中redis的使用和分布式session共享问题
对于分布式使用Nginx+Tomcat实现负载均衡,最常用的均衡算法有IP_Hash.轮训.根据权重.随机等.不管对于哪一种负载均衡算法,由于Nginx对不同的请求分发到某一个Tomcat,Tomca ...
- 玩转 SpringBoot 2 快速整合 | JSP 篇
前言 JavaServer Pages(JSP)技术使Web开发人员和设计人员能够快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面.作为Java技术系列的一部分,JSP技术可以快速开发独立 ...
- Marco's Java【SpringBoot入门(六) 之 Thymeleaf模板引擎的使用】
前言 本节呢给大家介绍一个新鲜 "玩意儿" 叫做Thymeleaf,Thymeleaf翻译过来就是 "百里香叶" 的意思 我发现这些大佬儿特别喜欢用叶子作为标识 ...
- springboot整合mysql5.7_详解SpringBoot整合MyBatis详细教程
1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver 然后导入以下整合依赖 org.mybatis.spring.boo ...
- springboot处理参数再转发请求_SpringBoot图文教程6—SpringBoot中过滤器的使用
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文系列教程技术大纲 SpringBoot 图文教程系列文章 ...
- 218.94.78.76:20001/index.php,详解spring中使用Elasticsearch的实例教程
本篇文章主要介绍了详解spring中使用Elasticsearch的代码实现,具有一定的参考价值,有兴趣的可以了解一下 在使用Elasticsearch之前,先给大家聊一点干货. 1. ES和solr ...
- Java中List集合的三种遍历方式(全网最详)
Map集合:链接: Map集合的五种遍历方式及Treemap方法 Set集合:链接: Java中遍历Set集合的三种方法 TreeSet集合:链接: Java深入了解TreeSet,和迭代器遍历方法 ...
最新文章
- 【TX2】英伟达Nvidia TX2连接蓝牙设备
- 2000及2005通用分页
- 【语法解释】init
- 生成式对抗网络Generative Adversarial Networks(GANs)
- mellanox 网卡驱动_收购Mellanox之后 NVIDIA发布全球首款25G安全智能网卡
- 对数位dp的一些拙见
- [css] css中兼容ie浏览器的前缀是什么?
- jSignature签名的用法,一文教会你(二)后台代码
- 使用 Flex 开发 Web 服务客户端
- phpcms如何做企业站-- 替换首页最初操作
- 解决Linux Kettle出现闪退问题
- 语言翻译成汇编语言_编程语言分类
- 1995-2020年省级地级市环境污染数据集 三废排放、废气烟粉尘废水固体废物排放、空气质量AQI、空气污染物浓度PM2.5、环境投资保护治理数据
- 马翔:联想双态IT 助力企业IT新变革
- 新手教程,网站建设第一步:织梦之家Dede程序的下载和安装
- 爬虫小案例——爬取豆瓣电影《寄生虫》的短评
- K8S Ingress的安装与使用
- 走过30年岁月的WPS,它的复兴之路该怎么走
- Latex 设置表格字体垂直居中且水平居中,字体放在表格最中央之简单好用的方法
- 无线通讯及几种无线通讯之间区别