不用jsp之后 前端只用html跟ajax跟后台交互的话有些东西还是不是很方便,特别数据多了,所以需要模板,最近接触的是thymeleaf模板 这个跟jsp的用法还是有点差别的
就说下用的比较多的那几个吧
首先你得在你的html上加上约束
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
然后你就可以用th这个标签了
比如一张图片 从静态资源里面拿
<img th:src="@{/static/images/audit/logo.png}" class="logo" />
在input 框接东西
<input name="treatyType" id="treatyType" class="input-large required" required="true" type="text" th:value="${entity.treatyType}">
这个从域里面拿东西有点坑。。如果你是存到session域里面的话 就要这样
<input name="treatyType" id="treatyType" class="input-large required" required="true" type="text" th:value="${session.entity.treatyType}">
如果碰到报错,加个判单也是很有用的 ?代表判断,
<input name="treatyType" id="treatyType" class="input-large required" required="true" type="text" th:value="${session.entity?.treatyType}">
这是在标签里面,如果你要放在标签外面可以这样
<h1>Hello, [[${session.user.name}]]</h1>
还有,想在js里面使用时,不要忘记加个这个<script th:inline="javascript">
</script>
其他的 我也是没怎用过。如果还有兴趣了解更多,可与去看看下面这个网站,thymeleaf的官方api
http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#expressions-on-selections-asterisk-syntax