springboot整合thymeleaf及常用标签的使用方法

27 篇文章 0 订阅
3 篇文章 0 订阅

请结合springboot学习教程项目github地址 https://github.com/heng1234/spring-boot_one来理解

 

pom.xml

<!-- 引入 thymeleaf 模板依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		

 

application.properties

#资源文件配置
############################################################
#
# thymeleaf 静态资源配置
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.thymeleaf.cache=false

Controller

package com.yh.controller;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yh.entity.User;

@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {

	@RequestMapping("finduser")
	public String finduser(ModelMap map) {
		User u = new User();
		u.setName("heng");
		u.setAge(21);
		u.setUrl("https://blog.csdn.net/qq_39313596");
		u.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
		map.addAttribute("u",u);
		
		java.util.List<User> list = new ArrayList<User>();
		User u2 = new User();
		u2.setName("heng");
		u2.setAge(17);
		u2.setUrl("https://blog.csdn.net/qq_39313596");
		u2.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
		User u3 = new User();
		u3.setName("heng");
		u3.setAge(19);
		u3.setUrl("https://blog.csdn.net/qq_39313596");
		u3.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
		User u4 = new User();
		u4.setName("heng");
		u4.setAge(21);
		u4.setUrl("https://blog.csdn.net/qq_39313596");
		u4.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
		list.add(u2);
		list.add(u3);
		list.add(u4);
		map.addAttribute("list", list);
		return "/thymeleaf/index";
	}
	@PostMapping("saveUser")
	
	public String saveUser(User u) {
		System.out.println(u);
		
		return "redirect:../thymeleaf/finduser";
	}
}

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<!-- 	<script th:src="@{/static/js/test.js}"></script> --> <!-- 引入js文件 需要在后端资源文件配置thymeleaf引入静态文件  -->
<body>
<p th:text="${u.name}"></p>
<p th:text="${u.age}"></p>
<p th:text="${u.url}"></p>
<p th:text="${u.desc}"></p>
<p th:utext="${u.desc}"></p>



<div th:object="${u}">
 <p th:text="*{name}"></p>
<p th:text="*{age}"></p>
<p th:text="*{url}"></p>
<p th:text="*{desc}"></p>
<p th:utext="*{desc}"></p>

</div>
<form th:action="@{saveUser}" method="post" th:object="${u}" th:method="post">
 <input type="text" th:field="*{name}"/>
 <input type="text" th:field="*{age}"/>
 <input type="text" th:field="*{url}"/>
 <input type="text" th:field="*{desc}"/>
  <input type="submit"/>
</form>
<select>
     <option >选择框</option>
     <option th:selected="${u.name eq 'lee'}">lee</option> <!-- eq是等于的意思 -->
     <option th:selected="${u.name eq 'heng'}">heng</option>
     <option th:selected="${u.name eq 'LeeCX'}">LeeCX</option>
</select>
<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>年龄备注</th>
<!--         <th>生日</th>-->
     </tr>
    <tr th:each="person:${list}">
        <td th:text="${person.name}"></td>
        <td th:text="${person.age}"></td>
        <td th:text="${person.age gt 18} ? 你成熟了 : 你还年轻">18岁</td>
       <!--  <td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd hh:mm:ss')}"></td> -->
    </tr>
</table>
<br/>

<br/>
<div th:switch="${u.name}">
  <p th:case="'lee'">lee</p>
  <p th:case="A">普通管理员</p>
  <p th:case="heng">超级管理员</p>
  <p th:case="*">其他用户</p>
</div>
<br/>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值