Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

request.setAttribute("list", fooList);

It's thus "list":

${list}

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


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.

${requestScope.list}

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...