How to obtain request / session / servletcontext attribute in JSP using EL?

眉间皱痕 提交于 2019-12-17 17:58:07

问题


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

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);

Then in the JSP, I want:

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

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.


回答1:


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

request.setAttribute("list", fooList);

It's thus "list":

<c:forEach var="post" items="${list}">

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.

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.

<c:forEach var="post" items="${requestScope.list}">

See also:

  • Unified expression language in Java EE 5 tutorial


来源:https://stackoverflow.com/questions/3579172/how-to-obtain-request-session-servletcontext-attribute-in-jsp-using-el

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!