Thymaleaf表達式基本對象
模板引擎提供了一組內置的對象,這些內置的對象可以直接在模板中使用,這些對象由#號開始引用,我們比較常用的內置對象。
1.#request
相當于httpServletRequest 對象,這是3.x版本,若是2.x版本使用 #httpServletRequest
• ${#request.getContextPath()}
在頁面獲取應用的上下文根,一般在js中請求路徑中加上可以避免404
<script type="text/javascript" th:inline="javascript">
var contextPath =[[${#request.getContextPath()}]];
var url = contextPath + "/user/userInfo";
alert(url);
</script>
• ${#request.getAttribute("phone")}
如果后臺將數據傳到request中,可以通過該方式在頁面上獲取
2.#session
相當于HttpSession 對象,這是3.x版本,若是2.x版本使用#httpSession
• 在后臺ThymeleafController中的userInfo方法中向session中放數據
session.setAttribute("website","http://m.dabaquan.cn");
• 在user.html中從session中取數據
<p th:text="${#session.getAttribute('website')}"></p>
Thymaleaf表達式功能對象
模板引擎提供的一組功能性內置對象,可以在模板中直接使用這些對象提供的功能方法;
工作中常使用的數據類型,如集合,時間,數值,可以使用Thymeleaf的提供的功能性對象來處理它們;
內置功能對象前都需要加#號,內置對象一般都以s結尾;
官方手冊:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
#dates: java.util.Date對象的實用方法,
#calendars: 和dates類似, 但是 java.util.Calendar 對象;
#numbers: 格式化數字對象的實用方法;
#strings: 字符串對象的實用方法: contains, startsWith, prepending/appending等;
#objects: 對objects操作的實用方法;
#bools: 對布爾值求值的實用方法;
#arrays: 數組的實用方法;
#lists: list的實用方法,比如#sets: set的實用方法;#maps: map的實用方法;#aggregates: 對數組或集合創建聚合的實用方法;