EL 将使用哪个范围(应用程序、servletContext、httpSession)来解释属性 [英] which scope (application, servletContext, httpSession) will EL use for interpreting attributes

查看:38
本文介绍了EL 将使用哪个范围(应用程序、servletContext、httpSession)来解释属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 jsp 中使用 <c:out value="${track}"> 时,属性 track 应该位于 (servletContext,httpSession 和请求)?

when I use <c:out value="${track}"> in a jsp, where should the attribute track be located in (servletContext, httpSession and request)?

我试图让一个控制器将属性 track 设置为 httpSession,但是 ${track} 在 .jsp 中没有给我任何东西.另一方面,如果我将它设置为 servletContext,${track} 会给我这个值.好像不太对.你能给出在.jsp(使用jstl)和控制器(.java)之间传递属性的方向吗?提前致谢.

I tried to have a controller to set the attribute track to the httpSession, but then ${track} doesn't give me anything in the .jsp. On the other hand, if I set it to the servletContext, ${track} give me the value. It doesn't seem right. Can you give a direction on passing attributes between .jsp (using jstl) and controllers (.java)? Thanks in advance.

推荐答案

它将在后台使用 JspContext#findAttribute() 来查找属性.链接的 javadoc 提到以下内容:

It will under the hoods use JspContext#findAttribute() to find the attribute. The linked javadoc mentions the following:

按顺序在页面、请求、会话(如果有效)和应用程序范围中搜索命名属性,并返回关联的值或 null.

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

因此,它将返回按页面、请求、会话和应用程序(servletcontext)范围的顺序搜索后找到的第一个非空值.

So, it will return the first non-null value which is found after searching in the order of page, request, session and application (servletcontext) scopes.

如果您在多个作用域中具有相同名称的属性和/或您想从特定作用域获取属性,那么您可以通过 ${pageScope}${requestScope}${sessionScope} 和/或 ${applicationScope}.例如

If you have attributes with the same name in multiple scopes and/or you'd like to get the attribute from a specific scope, then you can access it by the attribute maps available by the ${pageScope}, ${requestScope}, ${sessionScope} and/or ${applicationScope}. E.g.

${requestScope.track}

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆