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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arik~朽木

你的鼓励是我创作最大的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值