You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2015/10/03 17:50:31 UTC

Accessing action properties from JSPs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

All,

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.

(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?).

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJWD/lHAAoJEBzwKT+lPKRYAjkQAMfH92JS4kudGX8KJg1FH2zx
FC0Uvz4GZWtDDZhBy5EPZOuoLZhZevheejQq5dT9uRlWJaSBB39OrT2Lcdv+jNdL
vSQoP8bSQiHMD76SWe5GfeFcqTH0zNDutmrEiu+YjKcKcWIysDMDVveDrnKvWTQh
ZB13yys4HaDvE6r/gTxevVExSKcEe5s8GD/HRS9xtF5yjbKbA15i/LXa4XsnQvi+
guacbXPOewvs9CuRzV1JCR1y4YxJfjb+FJXnO3WtN3V5+kPHj/gE9ne/QcJHsQ2h
BOLjNve62kt2KRuiDA86c2cESD9Zv763udDfZ1LKI4q8xq4YaFl3uDFnwgB4zgxx
aIs2ftNnfMdhNXYlWSOFQL1HVA0iY78zsLdw5Xzu7Z9Jhioo20pxnt2PsiFdwa4z
jkMQAblJcmNCxHkVk1soayzv936GUVqvCIZbjqSQs/oPwZCpJF/fFmsmTTN/G4+R
iWXo0G9M/Zq9HpcmdXVlpjiGxW7EjTt6bYiqoKi0psvV3Rmy7UPVQUHh8Z9hKGz3
j5Wvl0m0Zhaf7dWMFpl5FnUICtOi1WUYUT6EQa3+usjEVdvoG/aB+6qreCl4G3Lo
1EU8lmT0IRORw6MaxZft152wCLPuGqiS3J+EFDzEexoPMd7M2Z37VF1KwogPA1N/
AJeYTvrTp5EillHT4ulu
=QyOq
-----END PGP SIGNATURE-----

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


RE: Accessing action properties from JSPs

Posted by Martin Gainty <mg...@hotmail.com>.
> To: user@struts.apache.org
> From: chris@christopherschultz.net
> Subject: Accessing action properties from JSPs
> Date: Sat, 3 Oct 2015 11:50:31 -0400
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> All,
> 
> 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?
MG>the <result...>jsp</result> is 'bound' inside struts-config.xml to parent <action tag
https://struts.apache.org/docs/action-configuration.html
MG>if on the other hand you want to use a different method to execute then you can use DMI
https://cwiki.apache.org/confluence/display/WW/Action+Configuration#ActionConfiguration-DynamicMethodInvocation
> 
> 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.
MG>s2 tag resolution provides access to struts value stack via OGNL expression(s)
> 
> Any suggestions are appreciated.
> 
> (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?
MG>in that case struts resolves this as a property placed on ValueStack
https://struts.apache.org/docs/tag-syntax.html

Does S2 export
> all my beans from the action (and other places?) to the request
> attributes?).
MG>if specified by scope attrib e.g. <s:set ...scope="request"/>
https://struts.apache.org/docs/set.html

> 
> Thanks,
> - -chris
MG>does that answer your understanding of struts-tags?

> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
> 
> iQIcBAEBCAAGBQJWD/lHAAoJEBzwKT+lPKRYAjkQAMfH92JS4kudGX8KJg1FH2zx
> FC0Uvz4GZWtDDZhBy5EPZOuoLZhZevheejQq5dT9uRlWJaSBB39OrT2Lcdv+jNdL
> vSQoP8bSQiHMD76SWe5GfeFcqTH0zNDutmrEiu+YjKcKcWIysDMDVveDrnKvWTQh
> ZB13yys4HaDvE6r/gTxevVExSKcEe5s8GD/HRS9xtF5yjbKbA15i/LXa4XsnQvi+
> guacbXPOewvs9CuRzV1JCR1y4YxJfjb+FJXnO3WtN3V5+kPHj/gE9ne/QcJHsQ2h
> BOLjNve62kt2KRuiDA86c2cESD9Zv763udDfZ1LKI4q8xq4YaFl3uDFnwgB4zgxx
> aIs2ftNnfMdhNXYlWSOFQL1HVA0iY78zsLdw5Xzu7Z9Jhioo20pxnt2PsiFdwa4z
> jkMQAblJcmNCxHkVk1soayzv936GUVqvCIZbjqSQs/oPwZCpJF/fFmsmTTN/G4+R
> iWXo0G9M/Zq9HpcmdXVlpjiGxW7EjTt6bYiqoKi0psvV3Rmy7UPVQUHh8Z9hKGz3
> j5Wvl0m0Zhaf7dWMFpl5FnUICtOi1WUYUT6EQa3+usjEVdvoG/aB+6qreCl4G3Lo
> 1EU8lmT0IRORw6MaxZft152wCLPuGqiS3J+EFDzEexoPMd7M2Z37VF1KwogPA1N/
> AJeYTvrTp5EillHT4ulu
> =QyOq
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  

Re: Accessing action properties from JSPs

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Lukasz,

On 10/4/15 3:49 AM, Lukasz Lenart wrote:
> 2015-10-03 17:50 GMT+02:00 Christopher Schultz
> <ch...@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/ap
ache/struts2/dispatcher/StrutsRequestWrapper.java#L72-L104

Thanks
> 
for the explanation. I'm very happy that this works... the
reference I provided at the top of my post suggests that working with
non-Struts2-provided tags could be difficult, but it hasn't been
difficult at all.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJWEW3YAAoJEBzwKT+lPKRYFSAQALqOJtcI6DxVdICqn4QVZB28
gtq51KVfs9EjaEBiuX4VqHiXOwDllegE2ruf18fvwZaD35lAeW22SRDcy0EquhAJ
qcWwdl3qxT9ge/sTgN3LaLpCJsJRgL+/IoSJ3MPH7/VhEPsM2roK/G2fcs5d9SQS
zGtGB09HEWuSL8Vm8KfIg2MdnejCDuWG0nrQcf6765C5sMTUTPYuNo+GgpBbjuad
zoyV6Lx7GhkQsrIUK4RDcMbKy7vd3In+2BmwDWR6hbMD2c5Nlbp7jJbpGaz760wS
itVi6TLslzASL3nBcmfOKv4NoEbOl6eT4/rOYbc8+9d6umMDTMa0OnDRnBl+FHVT
LHT/dwTlKThEGTKry74giR9xVV7kwnmk0Q5e3LmUns3gwYTMYOKHsOclVO6Bui6S
YyLjh7yfKi5vK/XNiaMNABkJOV/bLr46GYt9aA1X/tStV1EFbj3GQrZOHNPO+CPz
fqDctZkasuTUERhcp5lHRkekJhzLAjZtKPxacLWG4TQsdaYLTpHOSrvEGa5FEN2j
bBMGuDz7EVqOvGrXz2FdkP9e1oSCerQ1SBEWH9sr4yCwfIxkdyRr+zsz1pOlhD8o
lpSnUaViD7XJlZ8jKmaxMrk5xXRWzvvEujQlzIwAv2yNQm4XoRTrMa5kLl1qGaHH
fgcO2slGew8p5aSDibPd
=Bi6o
-----END PGP SIGNATURE-----

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


Re: Accessing action properties from JSPs

Posted by Lukasz Lenart <lu...@apache.org>.
2015-10-03 17:50 GMT+02:00 Christopher Schultz <ch...@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