目录
xml上需要加上
<html lang="en" xmlns:th="http://www.thymeleaf.org">
才可以使用thymeleaf技术
在src/main/resources/下创建templates目录
该目录是安全的,不允许外界直接通过url访问
在页面输出值
th:text
将值放入到input标签的value中
th:value
thymeleaf内置对象
①调用内置对象要加#号
②大部分内置对象以s结尾 如strings numbers dates
判断字符串是否为空
${#strings.isEmpty(key)}
判断字符串是否为空,为空返回true 不为空返回false
${#strings.contains(msg,'1')
判断字符串是否包含指定子串,如果包含返回true 否则返回false
${#strings.startsWith(msg,'h')
判断字符串是否以指定的子串开头 如果是返回true,否则返回false
${#strings.endsWith(msg,'h')
判断字符串是否以指定的子串结尾 如果是返回true,否则返回false
${#strings.length(msg)
获取字符串的长度
${#strings.indexOf(msg,'h')
查找子串的位置,并返回改字串的下标,没有找到返回-1
${#strings.substring(msg,10) 从指定位置开始截取字符串到末尾
${#strings.substring(msg,10,12) 从指定位置开始截取字符串到指定下标位置
${#strings.toUpperCase(msg) 将字符串的小写字母转成大写
${#strings.toLowerCase(msg) 将字符串的大写字母转成小写
日期格式化处理
${#dates.format(time)}
格式化日期,按照浏览器格式输出
${#dates.format(time,'yyyy-MM-dd')}
格式化日期,按照指定格式输出
<span th:text="${#dates.year(time)}"></span>
<span th:text="${#dates.month(time)}"></span>
<span th:text="${#dates.day(time)}"></span>
取出时间中的年 、月、日
条件判断
①th:if
<span th:if="${sex}=='男'">男</span>
<span th:if="${sex}=='女'">女</span>
②th:switch th:case
<div th:switch="${id}">
<span th:case="1">id为1</span>
<span th:case="2">id为2</span>
<span th:case="3">id为3</span>
</div>
迭代遍历
①th:each
②th:each 下的状态变量
Index 当前遍历对象的下标 从0开始
Count 当前迭代对象的计数 从1开始
Size 当前迭代对象长度
Even /old 当前迭代对象是否是偶数/奇数 布尔值 从0开始
First 当前循环是否是第一条 是返回true 否则返回false
Last 当前循环是否是最后一条 是返回true 否则返回false
<table border="1px" width="300px" >
<tr>
<td>ID</td>
<td>NAME</td>
<td>ADDRESS</td>
</tr>
<tr th:each="u : ${list}">
<td th:text="${u.id}"></td>
<td th:text="${u.name}"></td>
<td th:text="${u.address}"></td>
</tr>
</table>
③ th:each遍历map
@RequestMapping("/show3")
public String showInfo3(Model model){
Map<String ,User> map=new HashMap<>();
map.put("k1",new User(1,"lisi","jiangsu"));
map.put("k2",new User(2,"si","jiangsu"));
map.put("k3",new User(3,"lisi","jiangsu"));
model.addAttribute("map",map);
return "/view/index4";
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1px" width="300px" >
<tr>
<td>ID</td>
<td>NAME</td>
<td>ADDRESS</td>
</tr>
<tr th:each="maps : ${map}">
<td th:each="m : ${maps}" th:text="${m.value.id}"></td>
<td th:each="m : ${maps}" th:text="${m.value.name}"></td>
<td th:each="m : ${maps}" th:text="${m.value.address}"></td>
</tr>
</table>
</body>
</html>
域对象操作
①HttpServletRequest
${#httpServletRequest.getAttribute('key')
②HttpSession
${session.key}
③ServletContext
${application.key}
@RequestMapping("/show4")
public String showInfo4(HttpServletRequest request, Model model){
request.setAttribute("request","request");
request.getSession().setAttribute("session","httpSession");
request.getSession().getServletContext().setAttribute("app","application");
return "/view/index5";
}
@RequestMapping("/show4")
public String showInfo4(HttpServletRequest request, Model model){
request.setAttribute("request","request");
request.getSession().setAttribute("session","httpSession");
request.getSession().getServletContext().setAttribute("app","application");
return "/view/index5";
}
URL表达式
①基本语法
@{}
②绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径</a>
<a href="http://www.baidu.com">绝对路径</a>
③相对路径
--相对于项目的根
<a th:href="@{/info}">相对路径</a>
-相对于服务器路径的根
<a th:href="@{~/项目名/访问路径}">相对服务器的根</a>
④如何通过url在路径上传参数
<a th:href="@{/getInfo(id=1,name='lisi')}">路径传参</a>
绑定数据 传参
@Controller
public class TestController {
@GetMapping("/test")
public String test(Model model){
model.addAttribute("username","万腾飞");
return "test";
}
//使用内置对象 传值
@GetMapping("/t1")
public String test1(HttpServletRequest request){
HttpSession session = request.getSession();
session.setAttribute("username","万腾飞");
return "test";
}
//用ModelAndView绑定数据并跳转页面
@GetMapping("/t2")
public ModelAndView test2(ModelAndView modelAndView){
modelAndView.addObject("userinfo","电子商务");
modelAndView.setViewName("test");
return modelAndView;
}
}