SpringBoot中跳转html页面并渲染model中的内容
SpringBoot中跳转html页面并渲染model中的内容,简单易懂
0.环境及前提
1. 开发环境
- idea,jdk1.8(不限制版本),SpringBoot
2. 前提
- 请求参数没有问题;
- 业务逻辑没有问题;
- 可以正常获得数据;
- 有需要跳转的页面。
1.包结构
前提页面的位置:resources/static/index.html
需要跳转的页面:resources/templates/test.html
2.各页面代码
1.static/index.html页面代码
这里只用于跳转
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>页面一:static/index.html</h1>
<h1><a href="/test">点击跳转到页面二</a></h1>
</body>
</html>
2.templates/test.html页面代码
这里用于接收后台传来的Model内容的渲染
重点:
使用th:value="${name}"
属性将Model
的内容渲染为input
的value
报错请Alt+回车
引入依赖
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>欢迎来到页面二:templates/test.html</h1>
<h1>姓名:<input type="text" th:value="${name}"></h1>
<h1>性别:<input type="text" th:value="${sex}"></h1>
</body>
</html>
3.SpringBootController代码
这里是控制器,将name
和sex
通过model
传递给前端
package com.zixiu.demo_th.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/** * Description: 本类用于SpringBoot中跳转html页面并渲染model中的内容;控制器 * @author zixiu on 2020/1/9 18:55 */
@Controller
public class SpringBootController {
@GetMapping("/test")
public String test(Model model){
String name="zixiu";
String sex="男";
model.addAttribute("name",name);
model.addAttribute("sex",sex);
return "test";
}
}
3.在pom.xml中引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4.运行效果
跑起来。。。
首页效果:
需要跳转的页面:
这里可以看到html已经正常解析model的内容并渲染。
5.SpringBoot(thymeleaf)中常用th标签
关键字 | 功能介绍 | 案例 |
---|---|---|
th:id |
替换id | <input th:id="'xxx' + ${collect.id}"/> |
th:text |
文本替换 | <p th:text="${collect.description}">description</p> |
th:utext |
支持html的文本替换 | <p th:utext="${htmlcontent}">content</p> |
th:object |
替换对象 | <div th:object="${session.user}"> |
th:value |
属性赋值 | <input th:value = "${user.name}" /> |
th:with |
变量赋值运算 | <div th:with="isEvens = ${prodStat.count}%2 == 0"></div> |
th:style |
设置样式 | <div th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"></div> |
th:onclick |
点击事件 | <td th:onclick = "'getCollect()'"></td> |
th:each |
属性赋值 | <tr th:each = "user,userStat:${users}"> |
th:if |
判断条件 | <a th:if = "${userId == collect.userId}"> |
th:unless |
和th:if判断相反 | <a th:href="@{/login} th:unless=${session.user != null}">Login</a> |
th:href |
链接地址 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> |
th:switch |
多选择配合th:case使用 | <div th:switch="${user.role}"> |
th:case |
th:switch的一个分支 | <p th:case = "'admin'">User is an administrator</p> |
th:includ |
布局标签,替换内容到引入的文件 | <head th:include="layout :: htmlhead" th:with="title='xx'"></head> |
th:replace |
布局标签,替换整个标签到引入的文件 | <div th:replace="fragments/header :: title"></div> |
th:selectd |
selected选择框选中 | th:selected="(${xxx.id} == ${configObj.dd})" |
th:src |
图片类地址引入 | <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /> |
th:inline |
定义js脚本可以使用变量 | <script type="text/javascript" th:inline="javascript"> |
th:action |
表单提交的地址 | <form action="subscribe.html" th:action="@{/subscribe}"> |
th:remove |
删除某个属性 | <tr th:remove="all"> 1.all:删除包含标签和所有的孩子。2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。 |
th:attr |
设置标签属性,多个属性可以用逗号分隔 | 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。 |
最后:如果有问题请及时评论。
正文到此结束
- 本文标签: Spring Boot Java
- 版权声明: 本站原创文章,于2020年07月15日由子休发布,转载请注明出处