如何使用EL获取JSP中的request/session/servletcontext属性? [英] How to obtain request / session / servletcontext attribute in JSP using EL?

查看:390
本文介绍了如何使用EL获取JSP中的request/session/servletcontext属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这并不难,但是我没有任何运气.

I know this isn't hard, but I'm not having any luck.

我想通过JSP中的Servlet提供fooList.因此,在Servlet中,我有:

I want to make fooList from a Servlet available in a JSP. So in the Servlet I have:

request.setAttribute("list", fooList);
RequestDispatcher dispatcher = 
  getServletContext().getRequestDispatcher("/myJsp.jsp");
dispatcher.forward(request, response);

然后在JSP中,我想要:

Then in the JSP, I want:

<c:forEach var="post" items="${SOME_EL_HERE}">
    <!-- stuff -->
</c:forEach>

其中SOME_EL_HERE是一个表达式,用于检索我在request上设置的属性.

Where SOME_EL_HERE is an expression that retrieves the attribute that I have set on the request.

有什么想法吗?我的首选是不要通过添加框架使简单的任务复杂化,但是我愿意接受战略上的改变.

Any thoughts? My preference is to not complicate a simple task by adding a framework, but I'm open to changes in strategy.

推荐答案

这只是您在此处设置的属性 name :

It's just the attribute name as you've set yourself here:

request.setAttribute("list", fooList);

因此是"list":

${list}

对于session.setAttribute("name", value)application.setAttribute("name", value),其工作方式相同.该值在EL中仅${name}可用.

This works the same way for session.setAttribute("name", value) and application.setAttribute("name", value). The value is in EL available by just ${name}.

更多详细信息:EL默认情况下使用

More detail: EL uses by default PageContext#findAttribute() which scans in subsequently the page, request, session and application scopes for the firstnext non-null attribute value matching the given attribute name.

如果要为具有多个相同名称的属性在不同范围中显式指定范围,则通常的方法是使用${pageScope}${requestScope}${sessionScope}.例如

If you'd like to explicitly specify the scope for the case that you've multiple attributes with the same name in different scopes, then normal approach is to use ${pageScope}, ${requestScope}, ${sessionScope} or ${applicationScope}. E.g.

${requestScope.list}

另请参见:

  • Java EE 5教程中的统一表达语言
  • See also:

    • Unified expression language in Java EE 5 tutorial
    • 这篇关于如何使用EL获取JSP中的request/session/servletcontext属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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