SpringMVC集成Thymeleaf,從模板中獲取數據session、request參數

轉載自https://blog.csdn.net/sun1021873926/article/details/61615219
Request參數
請求的url爲:/user/get?id=12
訪問參數id可以使用param前綴
<p th:text="${param.q[0]}" th:unless="${param.q == null}">11</p>
例子中有兩點需要注意的地方:
${param.id!=null}檢查set中是否有參數id
參數是一個數組,因爲它可以多值比如?id=a&name=test
還有一種訪問方式是使用#httpServletRequest對象,可以直接進入javax.servlet.http.HttpServletRequest對象:
<p th:text="${#httpServletRequest.getParameter('id')}" th:unless="${#httpServletRequest.getParameter('id') == null}">11</p>

Session屬性
比如爲session添加了一個sessionValue屬性:

和Request參數訪問方式類似,這裏使用session前綴:
<divth:text="${session.sessionValue}">[...]</div>
同樣的,還可以使用#httpSession方式訪問,它直接進入javax.servlet.http.HttpSession對象。

ServletContext屬性
ServletContext屬性可以再request和session中共享,未來訪問ServletContext屬性,可以使用application前綴:


Spring beans
Thymeleaf可以通過@beanName訪問Spring應用上下午中註冊的bean,如
<div th:text="${@urlService.getApplicationUrl()}">...</div>
在這個例子中,@urlService就是在上下文中註冊的Spring Bean:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章