Thymeleaf模板使用实例+模板分页
1. 概述
在web开发中,我们经常会将公共头,公共尾,菜单等部分提取成模板供其它页面使用。在thymeleaf中,通过th:fragment、th:include、th:replace、参数化模板配置、css选择器加载代码块等实现。下文通过例子来说明用法:
- fragment语法
- 通过 th:fragment 和 css选择器加载代码块
- th:include 和 th:replace
- 参数化模板配置
Thymeleaf基本搭建和配置可以参考其他网站的配置实例,这里不详细说明。
2.引入头文件
首先我们讲述web项目里的js,css如何放到一个公共页面,用thymeleaf模板语法调用呢?其实thymeleaf里有个标签可以使用:
initHead.html
<th:block id="initHead"><!-- 可以充当HTML标签,但在模板执行后该标签会消失 -->
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<script type="text/javascript" th:src="@{/resources/js/share/backstage/jquery.js}"></script><!-- jqueryUI -->
<script type="text/javascript" th:src="@{/resources/js/share/jquery-ui-1.12.1.custom/external/jquery/jquery.js}"></script>
<link th:href="@{/resources/js/share/jquery-ui-1.12.1.custom/jquery-ui.css}" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var rootPath = "/{项目名}";
</script>
</th:block>
homepage.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>主页</title>
<th:block th:replace="common/base/initHead :: #initHead"></th:block><!-- 调用模板 -->
<script language="javascript">
$(function() {});
</script>
</head>
<body class="homepage_body">....
common/base/initHead :: #initHead"></th:block><!-- 调用模板 -->
<script language="javascript">
$(function() {});
</script>
</head>
<body class="homepage_body">....
问题来了,我们如何找到initHead.html文件呢?
其实也是通过配置的thymeleaf路径下查找。例如我配置了
<!-- 使用thymeleaf解析 -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/thymeleaf/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML" /> <property name="cacheable" value="false" /> <property name="order" value="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}" /> <property name="characterEncoding" value="UTF-8"/>
</bean>
配置了thymeleaf查找路径是“/thymeleaf/”
加上红色部分的路径,那么initHead.html文件的路径就必须是“WebRoot/thymeleaf/common/base/initHead.html”
3.分页
分页我使用vue.js的分页插件
道理和上面一样,附上源码:
pageInfo.html
<th:block id="pagin" th:fragment="pagin (refreshForm)">
<style type="text/css">
.page {font-weight: 900;height: 40px;text-align: center;color: #888;margin: 20px auto 0;background: #f2f2f2;}
.pagelist {font-size: 0;background: #fff;height: 50px;line-height: 50px;}
.pagelist span {font-size: 14px;}
.pagelist .jump {border: 1px solid #ccc;padding: 5px 8px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;cursor: pointer;margin-left: 5px;}
.pagelist .bgprimary {cursor: default;color: #fff;background: #337ab7;border-color: #337ab7;}
.jumpinp input {width: 55px;height: 26px;font-size: 13px;border: 1px solid #ccc;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;text-align: center;}
.ellipsis {padding: 0px 8px;}
.jumppoint {margin-left: 30px;}
.pagelist .gobtn {font-size: 12px;}
.bgprimary {cursor: default;color: #fff;background: #337ab7;border-color: #337ab7;}
.pagelist .jump.disabled {pointer-events: none;background: #ddd;}
.page_select {width: 50px;height: 30px;border: 1px;border-style: solid;border-color: #d5d5d5;border-radius: 5px;}
</style>
<div id="pagePlugIn"><div><div class="page" v-show="show"><div class="pagelist"><span class="jump" :class="{disabled:pstart}" @click="pageUp()">上一页</span> <span v-show="current_page>5" class="jump" @click="jumpPage(1)">1</span> <span class="ellipsis" v-show="efont">...</span> <span class="jump" v-for="num in indexs" :class="{bgprimary:current_page==num}" @click="jumpPage(num)">{{num}}</span><span class="ellipsis" v-show="ebehind">...</span> <span :class="{disabled:pend}" class="jump" @click="pageDown()">下一页</span><span v-show="current_page<pages-4" class="jump" @click="jumpPage(pages)">{{pages}}</span> <span class="jumppoint" >共<span style="color: red;" th:text="${pageInfo.totalCount}"></span>条记录</span><span class="jumppoint">页大小:</span><select id="select_PageSize" onchange="select_PageSize();" class="page_select"><option value="5" th:selected="${pageInfo.pageSize eq 5}">5</option><option value="15" th:selected="${pageInfo.pageSize eq 15}">15</option><option value="20" th:selected="${pageInfo.pageSize eq 20}">20</option><option value="50" th:selected="${pageInfo.pageSize eq 50}">50</option><option value="100" th:selected="${pageInfo.pageSize eq 100}">100</option></select><span class="jumppoint">跳转到:</span><span class="jumpinp"><input type="text" v-model="changePage"></span> <span class="jump gobtn" @click="jumpPage(changePage)">跳转</span></div></div></div><input type="hidden" id="pageInfo_pageNum" th:value="${pageInfo.pageNum}" /><input type="hidden" id="pageInfo_pageSize" th:value="${pageInfo.pageSize}" />
</div>
<script type="text/javascript" th:src="@{/resources/js/share/pagePlug_in/vue.js}"></script>
<script type="text/javascript">
/*** 初始化分页插件*/
var newlist = new Vue({el : '#pagePlugIn',data : {current_page : [[${pageInfo.pageNum}]], //当前页pages : [[${pageInfo.totalPageCount}]], //总页数//pages : 3, //总页数changePage : '', //跳转页nowIndex : 1},computed : {show : function() {return true},pstart : function() {return this.current_page == 1;},pend : function() {return this.current_page == this.pages;},efont : function() {if (this.pages <= 7) return false;return this.current_page > 5},ebehind : function() {if (this.pages <= 7) return false;var nowAy = this.indexs;return nowAy[nowAy.length - 1] != this.pages;},indexs : function() {var left = 1,right = this.pages,ar = [];if (this.pages >= 7) {if (this.current_page > 5 && this.current_page < this.pages - 4) {left = Number(this.current_page) - 3;right = Number(this.current_page) + 3;} else {if (this.current_page <= 5) {left = 1;right = 7;} else {right = this.pages;left = this.pages - 6;}}}while (left <= right) {ar.push(left);left++;}return ar;},},methods : {jumpPage : function(id) {//跳转指定页数方法if(id){this.current_page = id;refreshList(id, $("#select_PageSize option:selected").val());}},pageUp : function(){//上一页this.current_page--;var pageSize = $("#pageInfo_pageSize").val();var pageNum = Number($("#pageInfo_pageNum").val()) - 1;refreshList(pageNum, pageSize);},pageDown : function(){//下一页this.current_page++;var pageSize = $("#pageInfo_pageSize").val();var pageNum = Number($("#pageInfo_pageNum").val()) + 1;refreshList(pageNum, pageSize);}},
});
//刷新列表
function refreshList(nextPageNum, pageSize){var refreshForm = '[[${refreshForm}]]';//console.log(nextPageNum+"::"+pageSize+"::"+refreshForm);if(nextPageNum && nextPageNum > 0 && pageSize && pageSize > 0){if(nextPageNum <= 0){nextPageNum = 1;}var totalPageCount = Number([[${pageInfo.totalPageCount}]]);if(nextPageNum >= totalPageCount){nextPageNum = totalPageCount;}$("#"+refreshForm + " #pageNum").val(nextPageNum);$("#"+refreshForm + " #pageSize").val(pageSize);$("#"+refreshForm).submit();}
}
//更新页大小
function select_PageSize(){refreshList([[${pageInfo.pageNum}]], $("#select_PageSize option:selected").val());
}
</script>
</th:block>
引用部分
userList.html
<body>
<div th:replace="common/base/pageInfo :: #pagin ('listForm')"></div>
<form th:action="@{/common/user/findUserList.action}" id="listForm" name="listForm" method="post" target="datagridFrame"><input type="hidden" id="pageNum" name="pageInfo.pageNum" th:value="${pageInfo.pageNum}" /><input type="hidden" id="pageSize" name="pageInfo.pageSize" th:value="${pageInfo.pageSize}" />
</form>
</body>
呈现:
感兴趣的朋友可以关注微信公众号(会定时推送新的知识):
Thymeleaf模板使用实例+模板分页相关推荐
- Thymeleaf常用语法:模板片断
系统中的很多页面有很多公共内容,例如菜单.页脚等,这些公共内容可以提取放在一个称为"模板片断"的公共页面里面,其它页面可以引用这个 "模板片断"内容. 一.模 ...
- thymeleaf模板html a标签,Thymeleaf常用语法:模板片断
Thymeleaf常用语法:模板片断 系统中的很多页面有很多公共内容,例如菜单.页脚等,这些公共内容可以提取放在一个称为"模板片断"的公共页面里面,其它页面可以引用这个 " ...
- 前端页面后台管理模板—代码实例
今天分享下"前端页面后台管理模板"这篇文章,文中根据实例编码详细介绍,或许对大家的编程之路有着一定的参考空间与使用价值,需要的朋友接下来跟着云南仟龙Mark一起学习一下吧.近期自身 ...
- 数据列表DataList模板之实例
1,数据列表DataList与重复列表Repeator很类似,但是DataList应用更广泛,因为他可以选择和修改数据项的内容. DataList的数据显示和布局与Repeator控件一样都是通过&q ...
- dedecms模板中首页实现分页的方法
今天小编给大家分享的是dedecms模板中首页实现分页的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧.一定会有所收获的哦. dedecms模板中首页如何实现 ...
- UI界面排版搞不定 ?看看这些优秀的实例模板,可临摹学习!
设计师总是不断遇到这样的问题:如何赋予不同内容以合适的外观?如何调动视觉元素.外在形式传达思维信息,与受众沟通?解决这些问题是设计的关键所在,也是设计师必备的基本功. 版式设计这些形式美法则本身就是设 ...
- js取thymeleaf值_26、模板引擎thymeleaf
相对html+js的传统设计,现在很多网站都采用div&css+标签化+模块化的设计.模板引擎根据一定的语义,将数据填充到模板中,产生最终的HTML页面.模板引擎主要分两种:客户端引擎和服务端 ...
- 客制化PO单据模板(实例)
[前言]今天研究了一整天的PO单据,终于让我开发出属于自己的单据样式,客户就可以用我开发的样式打印单据了.整个过程其实蛮简单的,是指因为自己刚开始摸索,又没有资料,根据前面的积累慢慢探究出来了.今天里 ...
- maven java archetype_使用Maven Archetype插件构建Maven工程原型模板的实例
创建原型模板 1.在空目录运行archetype:generate上面的命令,待下载完必要的jar包后,首先需要输入内置的原型编号: Choose archetype: 1: internal -&g ...
最新文章
- 必看干货|成为大数据专业人员必要且重要的7大技能
- vivo 互联网业务就近路由技术实战
- 基于Linux的集群系统(一)
- mysql v8 漏洞_mysql'密码安全 - osc_v8gts6gd的个人空间 - OSCHINA - 中文开源技术交流社区...
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误
- 白话Elasticsearch14-深度探秘搜索技术之基于multi_match 使用most_fields策略进行cross-fields search弊端
- 2021牛客OI赛前集训营-提高组(第五场)C-第K排列【dp】
- BZOJ5251:[九省联考2018]劈配——题解
- 基于JAVA+SpringMVC+Mybatis+MYSQL的二手房交易系统
- 读取无线手柄数据_全透外形,优秀手感,双平台通吃:倍思Switch无线手柄
- 平时有没有使用xml和json
- 一级计算机办公软件,计算机一级与高级办公软件
- 猫途鹰公布2019年“旅行者之选”全球最佳海滩榜单
- 2021年PMP考试模拟题6(含答案解析)
- 数据库实验7---数据库的备份与恢复
- 用echarts实现水滴图效果
- LinuxDay12——磁盘存储和文件系统
- 教资必备的5大证件,丢失后该如何补办?
- 连接MySQL: Connections could not be acquired from the underlying database
- RTU远程终端控制系统S274