Thymeleaf+SpringMVC,从模板中获取数据session、request参数
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:
本文介绍如何在Thymeleaf+SpringMVC框架中通过模板获取session和request参数。具体包括如何利用param前缀访问URL参数、使用session前缀获取session属性,以及如何通过#httpServletRequest和#httpSession对象进行更灵活的数据访问。
812

被折叠的 条评论
为什么被折叠?



