转载自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前缀:
<div
th: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: