springboot集成thymeleaf模板引擎--记录

一、pom.xml引入jar包

        <!-- thymeleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>nz.net.ultraq.thymeleaf</groupId>
			<artifactId>thymeleaf-layout-dialect</artifactId>
		</dependency>

二、application.properties配置

#关闭thymeleaf缓存
spring.thymeleaf.cache=false

三、controller层代码

	@RequestMapping("/list")
	public String list(HttpServletRequest request, ModelMap map, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
		map.put("name", "hello world!");
		map.put("username", "周杰伦");
		map.put("date", new Date());
		map.put("sessionid", request.getSession().getId());
		map.put("nip", request.getRemoteAddr());
		map.put("rip", request.getHeader("X-Real-IP"));

		PageHelper.startPage(pageNum, 5);
		List<HashMap<String, Object>> list = userMapper.list();
		PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<HashMap<String, Object>>(list);
		map.put("pageInfo", pageInfo);

		return "demo";
	}

四、页面代码

<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" 
	xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="main">
<head>
    <meta charset="UTF-8"></meta>
    <title th:text="${name}"></title>
</head>
<body>
	<div layout:fragment="content">
	<!-- 内联 -->
	<script th:inline="javascript">
    	var name="hello,"+[[${username}]];
    	//alert(name);
    </script>
    
    <br/>
    <!-- 赋值、字符串拼接 -->
    [[${sessionid}]]<br>
    [[${nip}]]<br>
    [[${rip}]]<br>
    <p th:text="${username}">hello</p>
	<span th:text="'Welcome to our application,'+${username}+'!'"></span><br>
	<span th:text="|Welcome to our application,${username}!|"></span>
	
	<br/>
	<!-- 条件判断 -->
	<a th:if="${username eq '周杰伦'}" th:href="@{/1}">home</a>
	<a th:unless="${username eq '周杰伦'}" th:href="@{http://www.baidu.com/}">百度</a>
	
	<br/>
	<!-- 循环 -->
	权限
	<shiro:hasPermission name="add">增加</shiro:hasPermission>
	<shiro:hasPermission name="update">修改</shiro:hasPermission>
	<shiro:hasPermission name="delete">删除</shiro:hasPermission>
	<shiro:hasPermission name="query">查询</shiro:hasPermission>
	<br>
	角色
	<shiro:hasRole name="admin">admin</shiro:hasRole>
	<shiro:hasRole name="hr">hr</shiro:hasRole>
	<table border="1" cellspacing="0">
		<tr>
			<td>ID</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>地址</td>
			<td>电话</td>
			<td>状态变量:count</td>
			<td>状态变量:index</td>
			<td>状态变量:size</td>
			<td>状态变量:current</td>
			<td>状态变量:even</td>
			<td>状态变量:odd</td>
			<td>状态变量:first</td>
			<td>状态变量:last</td>
		</tr>
		<tr th:each="user,iterStat:${pageInfo.list}">
			<td th:text="${user.get('id')}"></td>
			<td th:text="${user.get('name')}"></td>
			<td th:text="${user.get('age')}"></td>
			<td th:text="${user.get('address')}"></td>
			<td th:text="${user.get('phone')}"></td>
			<td th:text="${iterStat.count}">状态变量:count</td>
			<td th:text="${iterStat.index}">状态变量:index</td>
			<td th:text="${iterStat.size}">状态变量:size</td>
			<td th:text="${iterStat.current.name}">状态变量:current</td>
			<td th:text="${iterStat.even}">状态变量:even</td>
			<td th:text="${iterStat.odd}">状态变量:odd</td>
			<td th:text="${iterStat.first}">状态变量:first</td>
			<td th:text="${iterStat.last}">状态变量:last</td>
		</tr>
	</table>
	<p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
    <a th:href="@{/list}">首页</a>
    <a th:href="@{/list(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
    <a th:href="@{/list(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
    <a th:href="@{/list(pageNum=${pageInfo.pages})}">尾页</a>
	
	<br>
	<br>
	<!-- 内嵌变量 dates -->
	<p th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}">时间</p>
	<p th:text="${#dates.createToday()}"></p>
	<p th:text="${#dates.createNow()}"></p>
	
	<!-- 内嵌变量 strings -->
	<p th:text="${#strings.isEmpty(username)}"></p>
	<p th:text="${#strings.listIsEmpty(list)}"></p>
	<p th:text="${#strings.length(username)}"></p>
	</div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/hjl0722/article/details/109641199