EL 将使用哪个范围(应用程序、servletContext、httpSession)来解释属性 [英] which scope (application, servletContext, httpSession) will EL use for interpreting attributes
问题描述
当我在 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}
另见:
- 我们的 EL wiki 页面
- 如何访问 EL 表达式语言 ${} 中的对象一个>
回到您的实际问题:如果您在访问会话范围属性时遇到问题,那么这只能意味着 JSP 没有使用与 servlet 使用的会话相同的会话.您可以通过在 servlet 中打印 Session ID 来调试它,如下所示
Back to your actual problem: if you have problems with accessing session scoped attributes, then it can only mean that the JSP is not using the same session as the servlet is using. You can debug it by printing the Session ID in servlet as follows
System.out.println(session.getId());
在 JSP 中由
${pageContext.session.id}
两者应该打印相同.如果不是,那么它肯定不是共享同一个会话.会话取决于域、上下文和 cookie.
Both should print the same. If not, then it is definitely not sharing the same session. The session is domain, context and cookie dependent.
您可以通过打印 ${sessionScope}
来显示所有可用的会话属性.它将以 AbstractMap#toString()
,包含所有会话属性.
You can display all available session attributes by just printing ${sessionScope}
. It will display a string in format as described in AbstractMap#toString()
, containing all session attributes.
${sessionScope}
这篇关于EL 将使用哪个范围(应用程序、servletContext、httpSession)来解释属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!