给下拉框赋值
1 2 3 4 |
<select id="type" name="type" > <option value="0">请选择</option> <option th:each="post:${posts}" th:value="${post.key}" th:text="${post.value}"></option> </select> |
给A标签赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<td><a th:href="@{/emp/findall?pageNum=1}">查询所有</a></td> <tr> <td><a href="findall?pageNum=1">首页</a></td> <td th:switch="${pageNum}"> <p th:case="1"> <a th:href="@{/emp/findall?pageNum=1}">上一页</a> </p> <p th:case="*"> <a th:href="@{/emp/findall(pageNum=${pageNum-1})}">上一页</a> </p> </td> <td th:switch="${pageNum}"> <p th:case="${count}"> <a th:href="@{/emp/findall(pageNum=${count})}">下一页</a> </p> <p th:case="*"> <a th:href="@{/emp/findall(pageNum=${pageNum+1})}">下一页</a> </p> </td> <td><a th:href="@{/emp/findall(pageNum=${count})}">尾页</a></td> </tr> |
加上如何做分页查询的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//查询所有 @RequestMapping("/findall") public String findAll(Model model,String pageNum){ if(pageNum==null){ pageNum="1";//从页面传来的数据取值,如果没有则默认为空 } int pagenum=Integer.parseInt(pageNum); int pageSize=2; //设置每页显示多少数据 int total=empService.findCount();//这里调用的是查询count(*) int pageCount=total%pageSize==0?total/pageSize:total/pageSize+1;//通过总记录数计算需要多少页 //这里调用这个PageHelper主要需要传递两个参数,当前页数,和每页大小 PageHelper.startPage(pagenum,pageSize); List<Emp> lists=empService.findAll(); //分别装入model中,以便在页面取值 model.addAttribute("emps",lists); model.addAttribute("pageNum",pagenum); model.addAttribute("count",pageCount); return "details"; } @Configuration public class PageConfiguration { @Bean public PageHelper pageHelper(){ System.out.println("PageConfiguration.pageHelper()"); PageHelper pageHelper=new PageHelper(); Properties p=new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWith", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } } |
给table循环赋值(其中包含了th:switch的用法,以及判断的用法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<table> <tr id="title"> <td colspan="7">雇员查询系统</td> </tr> <tr id="content"> <td>雇员编码</td> <td>雇员职位</td> <td>雇员姓名</td> <td>雇员性别</td> <td>雇员年龄</td> </tr> <tr th:each="emp:${emps}"> <td th:text="${emp.id}"></td> <td th:switch="${emp.post_type}"> <p th:case="1">行政经理</p> <p th:case="2">部门经理</p> <p th:case="3">总经理</p> </td> <td th:text="${emp.emp_name}"></td> <td th:text="${emp.emp_sex=1}?'男':'女'"></td> <td th:text="${emp.emp_age}"></td> </tr> <span style="color:red" th:text="${message}"></span> </table> |
补充一点从别的博客上看的
thymeleaf是一个支持html原型的自然引擎,它在html标签增加额外的属性来达到模板+数据的展示方式,由于浏览器解释html时,忽略未定义的标签属性,因此thymeleaf的模板可以静态运行。
th:text=”${data}”,将data的值替换该属性所在标签的body。字符常量要用引号,比如th:text=”‘hello world'”,th:text=”2011+3″,th:text=”‘my name is ‘+${user.name}”
th:utext,和th:text的区别是”unescaped text”。
th:with,定义变量,th:with=”isEven=${prodStat.count}%2==0″,定义多个变量可以用逗号分隔。
th:attr,设置标签属性,多个属性可以用逗号分隔,比如th:attr=”src=@{/image/aa.jpg},title=#{logo}”,此标签不太优雅,一般用的比较少。
th:[tagAttr],设置标签的各个属性,比如th:value,th:action等。
可以一次设置两个属性,比如:th:alt-title=”#{logo}”
对属性增加前缀和后缀,用th:attrappend,th:attrprepend,比如:th:attrappend=”class=${‘ ‘+cssStyle}”
对于属性是有些特定值的,比如checked属性,thymeleaf都采用bool值,比如th:checked=${user.isActive}
th:each, 循环,<tr th:each=”user,userStat:${users}”>,userStat是状态变量,有 index,count,size,current,even,odd,first,last等属性,如果没有显示设置状态变量,thymeleaf会默 认给个“变量名+Stat”的状态变量。
th:if or th:unless,条件判断,支持布尔值,数字(非零为true),字符,字符串等。
th:switch,th:case,选择语句。 th:case=”*”表示default case。
th:fragment,th:include,th:substituteby:fragment为片段标记,指定一个模板内一部分代码为一个片段,然后在其它的页面中用th:include或th:substituteby进行包含。
包含的格式为,格式内可以为表达式,比如th:include=”footer::$(user.logined)?’logined’:’notLogin'”:
“templatename::fragname”,指定模板内的指定片段。
“templateName::[domselector]”,指定模板的dom selector,被包含的模板内不需要th:fragment.
“templatename”,包含整个模板。
th:include和th:substituteby的区别在于前者包含片段的内容到当前标签内,后者是用整个片段(内容和上一层)替换当前标签(不仅仅是标签内容)。
th:remove=”all|body|tag|all-but-first”,一般用于将mock数据在真实环境中移除,all表示移除标签以及标签内容,body只移除内容,tag只移除所属标签,不移除内容,all-but-first,除第一条外其它不移除。
由 于一个标签内可以包含多个th:x属性,其先后顺序为:include,each,if/unless/switch/case,with,attr /attrprepend/attrappend,value/href,src ,etc,text/utext,fragment,remove。
内联文本:[[…]]内联文本的表示方式,使用时,必须先用th:inline=”text/javascript/none”激活,th:inline可以在父级标签内使用,甚至作为body的标签。内联文本尽管比th:text的代码少,但是不利于原型显示。
还有些很复杂的东西,没搞过,真的觉得用这个模版比较难,虽然有好处,但是作为新手从使用jsp过渡来,使用thymeleaf感觉很不顺手
当你使用这些引擎的任何一种,并采用默认的配置,你的模板将会从src/main/resources/templates目录下自动加载