目录
2、th::insert,th::replace,th::include的区别
1、初体验
thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。类似 JSP,Velocity,FreeMaker 等,它也可以轻易的与 Spring MVC 等 Web 框架进行集成作为 Web 应用的模板引擎。与其它模板引擎相比,
Thymeleaf 最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个 Web 应用。
2、怎么用
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
引入命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!-- 要加这玩意thymeleaf才能用 -->
2、已经自动注入,可以直接使用
具体bean的自动注入,可参阅下面一篇大佬的博客,了解为什么用的bean能直接@Autowired
3、表达式语法
4、怎么用
前端的thymeleaf怎么取数据?
1、springboot中可以使用model,modelandview,session,request都可以进行传值,其中model和modelandview其本质就是request
@Controller
public class ViewTestController {
@GetMapping("/hello")
public String hello(Model model){
//model中的数据会被放在请求域中 request.setAttribute("a",aa)
model.addAttribute("msg","一定要大力发展工业文化");
model.addAttribute("link","http://www.baidu.com");
return "success";
}
}
2、使用ajax和json传数据
package com.atqh.Servlet.huishouzhanServlet;
import com.atqh.been.MingPian;
import com.atqh.dao.IDeldao;
import com.atqh.dao.impl.IDelmpl;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
public class GetDel extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
IDeldao iDeldao = new IDelmpl();
PrintWriter writer = resp.getWriter();
String username = (String) req.getSession().getAttribute("username");
List<MingPian> del = iDeldao.getdel(username);
// System.out.println("!!!!!!!!!!!!!!!!!d调用了!!");
if (del.size() != 0) {
// req.setAttribute("del", del);
req.setCharacterEncoding("utf-8");
Gson gson = new Gson();
String data = gson.toJson(del);
System.out.println("我是string!!!!!" + data);
writer.write(data);
}
}
}
3、常用操作
设置属性值-th:attr
- 设置单个值
<form action="subscribe.html" th:attr="action=@{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
- 设置多个值
<img src="../../images/gtvglogo.png"
th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
thymeleaf内联写法
使用 [[${}]]
<p>Hello, [[${session.user.name}]]!</p>
其他几类基本运算符的用法
1、foreach遍历list,页面展示
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
2、If/Unless判定条件是否展示
Thymeleaf 中使用th:if
和th:unless
属性进行条件判断,下面的例子中,<a>
标签只有在th:if
中条件成立时才显示,unless与其相反:
<a th:href="@{/login}" th:if=${session.user==null}>Login</a>
3、Switch判断展示的object
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
4、内置对象
在Thymeleaf中的内置对象有:
#dates
:日期格式化内置对象,具体方法可以参照java.util.Date
;
#calendars
:类似于#dates
,但是是java.util.Calendar
类的方法;
#numbers
: 数字格式化;
#strings
:字符串格式化,具体方法可以参照java.lang.String
,如startsWith
、contains
等;
#objects
:参照java.lang.Object
;
#bools
:判断boolean类型的工具;
#arrays
:数组操作的工具;
#lists
:列表操作的工具,参照java.util.List
;
#sets
:Set操作工具,参照java.util.Set
;
#maps
:Map操作工具,参照java.util.Map
;
#aggregates
:操作数组或集合的工具;
#messages
:操作消息的工具。thymeleaf+bootstrap,onclick传参
4、防止表单重复提交
参考另一篇博客
5、抽取公共页面
1、创建引用的片段
1、使用th::fragment标签声明,如上所示
2、使用css选择器
2、th::insert,th::replace,th::include的区别
官方文档永远是最好的学习工具