struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: Accessing action properties from JSPs
Date Sun, 04 Oct 2015 07:49:22 GMT
2015-10-03 17:50 GMT+02:00 Christopher Schultz <chris@christopherschultz.net>:
> Is this the latest wisdom for accessing action properties from JSPs
> using EL?
>
> https://struts.apache.org/docs/access-to-valuestack-from-jsps.html
>
> So if my action has a "public String getItems)" method, I'd like to do
> something as close as possible to this:
>
> <c:forEach var="item" items="$action.items">
> </c:forEach>
>
> It looks like I can do this very quickly:
>
> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <s:set var="action" value="items" />
> <c:forEach var="item" value="${items}">
> ...
> </c:forEach>
>
> Is there something I can do to bind "action" to the actual action
> instance?
>
> Or I can use the Struts 2 "iterate" tag:
>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <s:iterator var="item" value="items">
> </s:iterator>
>
> I realize that this is a simple example, but is there a recommended
> technique, here? EL gives me the ability to do things like print
> values with somewhat less code: "${fn:escapeXml(foo)}" versus
> <s:property escapeHtml="true" value="foo" />
>
> In general, EL seems to be more universally understood, while the s2
> tags are a bit more specialized.
>
> Any suggestions are appreciated.

JSTL/EL are supported out-of-the-box

https://struts.apache.org/docs/can-we-use-jstl-with-the-framework.html

> (After writing all of the above and experimenting, I've found that I
> can reference ${items} in my JSP without doing anything with the
> Struts2 tags: no <s:set> required. How is that working? Does S2 export
> all my beans from the action (and other places?) to the request
> attributes?).

The magic happens in StrutsRequestWrapper (Struts wraps each request
with dedicated class). The wrapper first tries to fetch the attribute
from the original request, if not found, it will use ValueStack to
find the value.

https://github.com/apache/struts/blob/master/core/src/main/java/org/apache/struts2/dispatcher/StrutsRequestWrapper.java#L72-L104


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message