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的内容渲染为inputvalue
报错请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代码
这里是控制器,将namesex通过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:caseth: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:selectdselected选择框选中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}",此标签不太优雅,一般用的比较少。

最后:如果有问题请及时评论。

这是本人花了两周时间做出的小程序,感兴趣的可以扫一下,欢迎提取意见

点此链接查看小程序介绍:一款炫酷简洁的记账小程序

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在 Spring Boot ,可以使用 Thymeleaf 模板引擎来实现页面和参数传递。下面是实现方法: 1. 在 pom.xml 文件添加 Thymeleaf 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在 application.properties 文件配置 Thymeleaf 模板引擎: ```properties # Thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 3. 创建控制器类(如:ProductController),用于处理请求和返回页面。在该控制器,可以使用 @GetMapping 或 @PostMapping 注解来映射请求,同时使用 @RequestParam 或 @PathVariable 注解来获取请求参数。 ```java @Controller public class ProductController { @Autowired private ProductService productService; @GetMapping("/products") public String list(Model model) { List<Product> productList = productService.getAllProducts(); model.addAttribute("productList", productList); return "productList"; } @GetMapping("/products/{id}") public String detail(@PathVariable("id") Long id, Model model) { Product product = productService.getProductById(id); model.addAttribute("product", product); return "productDetail"; } @PostMapping("/products") public String add(Product product) { productService.addProduct(product); return "redirect:/products"; } // 其他方法省略... } ``` 4. 在 templates 目录下创建 HTML 模板文件,并使用 Thymeleaf 模板语言展示数据和页面。可以使用 th:text 和 th:each 等属性来展示数据,使用 th:href 属性来设置页面链接。 productList.html: ```html <!DOCTYPE html> <html> <head> <title>Product List</title> </head> <body> <h1>Product List</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Price</th> </tr> </thead> <tbody> <tr th:each="product : ${productList}"> <td th:text="${product.id}"></td> <td th:text="${product.name}"></td> <td th:text="${product.price}"></td> </tr> </tbody> </table> <a th:href="@{/products/add}">Add Product</a> </body> </html> ``` productDetail.html: ```html <!DOCTYPE html> <html> <head> <title>Product Detail</title> </head> <body> <h1>Product Detail</h1> <h2 th:text="${product.name}"></h2> <p th:text="${product.description}"></p> <p th:text="'Price: ' + ${product.price}"></p> <a th:href="@{/products}">Back</a> </body> </html> ``` 5. 在控制器调用模板文件,并将数据传递给模板渲染引擎: ```java @GetMapping("/products") public String list(Model model) { List<Product> productList = productService.getAllProducts(); model.addAttribute("productList", productList); return "productList"; } ``` 其,"productList" 是模板文件名,productList 是控制器方法的数据模型。 6. 最后,在浏览器访问页面,即可看到展示出来的数据和页面

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子休。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值