目前Java Web开发推荐使用模板模板引擎,不建议使用jsp页面html
模板引擎,不须要编译,速度快。
经常使用的模板引擎:Freemarker、Veloctiy、Thymeleaf等
SpringBoot推荐使用Thymeleaf,且默认不支持jsp页面,由于jsp必须打包。session
设置元素中的文本内容。
th:text对特俗字符进行转义,等价于[[${ }]]
th:utext 对特俗字符不进行转义 。等价于[(${})]app
<p th:if="${age>=18}">成年</p>
<p th:unless="${age>=24}">成年</p>
<p th:switch="${role}"> <span th:case="admin">管理员</span> <span th:case="teacher">教师</span> <span th:case="student">学生</span> <span th:case="*">其余</span> </p>
<ul> <li th:each="name:${students}" th:text="${name}"></li> </ul>
<form action="/modify" method="post" th:object="${user}"> 编号:<input type="text" th:field="*{id}" readonly><br> 姓名:<input type="text" th:field="*{name}"><br> 年龄: <input type="text" th:field="*{age}"><br> <input type="submit" value="修改"> </form>
<div th:include="/include/header::head"></div> 中间部分 <div th:include="/include/foot::copy"></div> <div th:insert="/include/foot::copy"></div> <div th:replace="/include/header::head"></div>
footless
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <footer th:fragment="copy"> 这是页面的底部 </footer> </html>
headerjsp
<!DOCTYPE html> <!---导入thymeleaf命名空间 --> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <header th:fragment="head"> 这是页面的头部 </header> </html>
使用内置的工具对象,如#strings、#data、#arrays、@list、#maps工具
须要和th:object配合使用,简化了对象属性的获取post
定义urlthis
<!DOCTYPE html> <!---导入thymeleaf命名空间 --> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div th:text="${name}"></div> <div>th:tex和th:utext区别</div> <div>utext</div> <div th:utext="${html}"></div> <div>text</div> <div th:text="${html}"></div> <div>[[${html}]]aa</div> <div>[(${html})]bb</div> <!---替换html的原生属性 --> <div th:id="${id}" th:title="${title}">这是一个div</div> <p th:if="${age>=18}">成年</p> <p th:unless="${age>=24}">成年</p> <p th:switch="${role}"> <span th:case="admin">管理员</span> <span th:case="teacher">教师</span> <span th:case="student">学生</span> <span th:case="*">其余</span> </p> <ul> <li th:each="name:${students}" th:text="${name}"></li> </ul> <h3>修改用户信息</h3> <form action="/modify" method="post" th:object="${user}"> 编号:<input type="text" th:field="*{id}" readonly><br> 姓名:<input type="text" th:field="*{name}"><br> 年龄: <input type="text" th:field="*{age}"><br> <input type="submit" value="修改"> </form> <!---${} 获取对象或属性的方法 --> <div th:text="${user.getName()}"></div> <div th:text="${user['age']}"></div> <!---获取集合 --> <div th:text="${users.size()}"></div> <div th:text="${users[1].getName()}"></div> <div th:text="${users[1].getName()}"></div> <div >元素个数[[${users.size()}]]</div> <div th:text="${users.get(1).name}"></div> <!---使用内置基本对象 --> <div th:text="${session.sex}"></div> <div th:text="${application.hbody}"></div> <!---使用内置的工具对象 --> <div th:text="${#strings.startsWith(user.name,'t')}"></div> <div>是否包含t [[${#strings.startsWith(user.name,'t')}]]</div> <div th:text="${#strings.substring(user.name,1,2)}"></div> <div th:text="${#strings.length(user.name)}"></div> <div th:text="${#dates.createNow()}"></div> <div th:text="${#dates.create(2018,1,2)}"></div> <div th:text="${#dates.format(#dates.createNow(),'yyyy-MM-dd')}"></div> <!---*{}选择表达式 --> <div>--------------------------------*{}选择表达式 ------------------------------------------------</div> <div th:object="${user}"> <div th:text="*{id}"></div> <div th:text="*{name}"></div> <div th:text="*{age}"></div> </div> <!---@{} url表达式 --> <a th:href="@{/finduser/(username=${user.name})}">查询指定的用户信息</a> <!---运算符 --> 性别: <input type="radio" name="sex" th:value="male" th:checked="${session.sex eq 'male'}">男 <input type="radio" name="sex" th:value="male" th:checked="${session.sex eq 'fmale'}">女 <div th:if="${address==null}">未找到信息</div> <div th:text="${users.size()>=2? '大于等于2' : '小于等于2'}"></div> <div></div> </body> </html>
controllerurl
model.addAttribute("name","toms"); model.addAttribute("html","<mark>您好,\\您好啊</mark>"); model.addAttribute("id","mydiv"); model.addAttribute("title","this is mytitle"); model.addAttribute("age",21); model.addAttribute("role","teacher"); model.addAttribute("students", Arrays.asList("tom","张三","李四")); User user = new User(1001, "tom", 21); model.addAttribute("user",user); List<User> users=new ArrayList<>(); users.add(new User(10002,"lihai",20)); users.add(new User(10003,"tom",30)); users.add(new User(10004,"cat",40)); model.addAttribute("users",users); session.setAttribute("sex","male"); session.getServletContext().setAttribute("hbody","gname"); return "success"; //使用thymeleaf 会自动拼写前缀和后缀