templates目录
该目录是是安全的,因为可以避免被外界直接访问,如果需要访问则需要借助于Controller去完成
thymeleaf
thymeleaf是通过特定的标记语法对html标记做渲染
Thymeleaf是面向 Web 和独立环境的现代服务器端Java模板引擎,能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。
Thymeleaf的主要目标是提供一个优雅和高度可维护的创建模板的方式。
** org.xml.sax.SAXParseException: 元素类型 “meta” 必须由匹配的结束标记 “” 终止。 ** 解决方案:
- 对meta标签进行闭合操作
- 改变thymeleaf和thymeleaf-layout-dialect版本依赖
- thymeleaf版本在3.0.x以上
- thymeleaf-layout-dialect在2.0.x以上
Thymeleaf语法
- ** th:text ** 在页面中输出值
<h2><span th:text="thymeleaf的消息"></span></h2>
<h2><span th:text="${msg}"></span></h2>
- ** th:value ** 可以将一个值放入到input标签的value中
<input type="text" th:value="${thymeleafMsg}">
- ** ${#strings.isEmpty(key)} ** 判断key值是否为空(如果指定字符串为空,则返回true,如果不为空,则返回false)
<h2><span th:text="${#strings.isEmpty(msg)}"></span></h2>
- ** 判断字符串是否包含指定内容(包含返回true,不包含返回false) **
- 第一个参数表示要对哪个key值进行查询
- 第二个参数表示要查找的内容
${#strings.contains(msg,'消息')}"
- ** 判断指定字符串内容是否以指定内容开头(如果是返回true,如果不是返回false) **
${#strings.startsWith(thymeleafMsg,'T')}
- ** 判断指定字符串内容是否以指定内容结尾(如果是返回true,如果不是返回false) **
${#strings.endsWith(thymeleafMsg,'消息')
- ** 计算指定字符串长度 **
${#strings.length(thymeleafMsg)}
- ** 查询内容在指定字符串中的索引位置(如果有多个,则只查询左起第一个的位置,如果没找到则返回-1) **
${#strings.indexOf(thymeleafMsg,'e')}
- ** 截取指定的字符内容 **
- 一个参数表示要从哪个索引位置开始截取,一直到字符串的最后
- 二个参数(第一个参数表示要从哪个索引位置开始截取,第二个参数表示从哪个索引位置结束)
${#strings.substring(thymeleafMsg,3)}
${#strings.substring(thymeleafMsg,3,8)}
- ** 将指定内容转为大写 **
${#strings.toUpperCase(thymeleafMsg)}
- ** 将指定内容转为小写 **
${#strings.toLowerCase(thymeleafMsg)}
- ** 转换时间格式 **
默认以浏览器的语言为格式化的标准,例:如果浏览器默认为中文,则显示北京时间
${#dates.format(date)}
${#dates.format(date,'yyyy-MM-dd HH:mm:ss')} <!-- 转换时间格式(以指定时间格式化进行格式化) -->
${#dates.year(date)} # 获取年份
${#dates.month(date)} # 获取月份
${#dates.day(date)} # 获取天数
- ** if判断 **
<span th:if="${sex} == '男'">
性别:男
</span>
- ** switch分支判断 **
<div th:switch="${number}">
<div th:case="1">编号为:1</div>
<div th:case="2">编号为:2</div>
<div th:case="3">编号为:3</div>
<div th:case="4">编号为:4</div>
<div th:case="5">编号为:5</div>
</div>
- ** each循环list **
- Java后端代码片段
@RequestMapping(value = "/eachList")
public String eachList(Model model) {
List<UserEntity> userEntity = new LinkedList<UserEntity>();
userEntity.add(new UserEntity(100,"张三","中国北京","男"));
userEntity.add(new UserEntity(101,"李四","中国武汉","男"));
userEntity.add(new UserEntity(102,"貂蝉","三国","女"));
userEntity.add(new UserEntity(103,"张三","中国北京","男"));
userEntity.add(new UserEntity(104,"王五","中国河南","男"));
model.addAttribute("userList",userEntity);
return "judge";
}
- html代码片段
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>地址</th>
<th>性别</th>
</tr>
<tr th:each="user : ${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.address}"></td>
<td th:text="${user.sex}"></td>
</tr>
</table>
- ** each循环Map **
- Java后端代码片段
@RequestMapping(value = "/eachMap")
public String eachMap(Model model) {
Map<String,Object> hashMap = new HashMap<String,Object>();
hashMap.put("user1", new UserEntity(105,"张三","中国北京","男"));
hashMap.put("user2", new UserEntity(106,"李四","中国武汉","男"));
hashMap.put("user3", new UserEntity(107,"貂蝉","三国","女"));
hashMap.put("user4", new UserEntity(108,"张三","中国北京","男"));
hashMap.put("user5", new UserEntity(109,"王五","中国河南","男"));
model.addAttribute("userMap",hashMap);
return "judge";
}
- html代码片段
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>地址</th>
<th>性别</th>
</tr>
<!-- each遍历Map -->
<tr th:each="userMap : ${userMap}">
<td th:each="user : ${userMap}" th:text="${user.value.id}"></td>
<td th:each="user : ${userMap}" th:text="${user.value.name}"></td>
<td th:each="user : ${userMap}" th:text="${user.value.address}"></td>
<td th:each="user : ${userMap}" th:text="${user.value.sex}"></td>
</tr>
</table>
- ** 获取作用域对象内容 **
- Java后端代码片段
@RequestMapping(value = "/domain")
public String domainObject(HttpServletRequest request) {
// 设置Request对象内容
request.setAttribute("request_msg", "Reqeust请求的内容");
// 设置Session对象内容
request.getSession().setAttribute("session_msg", "Session请求的内容");
// 设置Application对象内容
request.getSession().getServletContext().setAttribute("application_msg", "Application请求的内容");
return "domain";
}
- html代码片段
<table>
<tr>
<td>Request Scope Content:</td>
<td><span th:text="${#httpServletRequest.getAttribute('request_msg')}"></span></td>
</tr>
<tr>
<td>Sesssion Scope Content:</td>
<td><span th:text="${session.session_msg}"></span></td>
</tr>
<tr>
<td>Application Scope Content:</td>
<td><span th:text="${application.application_msg}"></span></td>
</tr>
</table>
- ** 状态变量属性 **
- ** index **:当前迭代器的索引,从0开始
- ** count **:当前迭代对象的计数,从1开始
- ** size **:被迭代对象的长度
- ** even / odd **:(布尔值)当前循环的次数是奇数还是偶数,从0开始
- ** first **:(布尔值)当前循环的记录是否是第一条记录,如果是显示true,否则显示false
- ** last **:(布尔值)当前循环的记录是否是最后一条记录,如果是显示true,否则显示false
** 注意语法: **
- 调用内置对象一定要用**#**号;
- 大部分内置对象都以s结尾,例如:strings/numbers/dates
更多的Thymeleaf信息可以参考《Thymeleaf教程》