You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Kropp, Henning" <hk...@microlution.de> on 2008/02/27 11:46:02 UTC

parameter to custom tag

Hi List,

I am new to struts2. I iterate over a list of strings. Every string in 
that list is supposed to be executed by a custom tag. I tried it in many 
fashions but its not working. I am only able to give over the whole list 
as a string with:

<custom:tag attr="${model.list}" />

How to I get the <s:proptery /> value into my custom tag?

Kind regards
Henning Kropp


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


Re: [struts] parameter to custom tag

Posted by "Kropp, Henning" <hk...@microlution.de>.
Thank you Dale!

Dale Newfield schrieb:
> Kropp, Henning wrote:
>> I must add that s:set printed out the string as well. I am not quit 
>> sure if its supposed to, but it did.
>
> I glossed over this--I wouldn't use s:set here, but rather c:set. 
> Neither one should print the value (are you sure you didn't do that 
> for debugging inside your custom tag?), but since you're trying to set 
> a value to be extracted with EL, I'd use the tag library that is 
> geared toward EL.
>
> That would be:
> <c:set var="stringArgForCustomTag"><s:property /></c:set>
> <custom:tag attr="${stringArgForCustomTag}"/>
>
> > My understanding of struts is not as experienced so if I may ask,
>> why would I need an extra jsp to pre-evaluate the OGNL as string as 
>> you proposed?
>
> You don't.  If you want to use EL exclusively to extract the data, 
> don't use s:iterator, but rather one of the c: loop tags, then you 
> won't need the "set".  If you want to use OGNL to name/manipulate/etc. 
> the data, then something needs to be able to take that OGNL expression 
> and evaluate it.  Your current solution to that is to have s:property 
> do the work, and have c:set store the results of that work in a place 
> that can then be handed off to your custom tag.  An alternate solution 
> would be to enable your custom tag to evaluate OGNL directly.  So 
> you'd probably have within your tag a String setter for attr that just 
> stores the string expression (argExpr below), then in your doTag (or 
> doStartTag, or whatever) do something like the following:
>
>     PageContext pageContext = (PageContext) getJspContext();
>     ValueStack valueStack = TagUtils.getStack(pageContext);
>
>     argValue = valueStack.findString(argExpr);
>
> to ask OGNL to evaluate the expression for you and give you the 
> resulting value.
>
> So, two different solutions, neither of which need the :set tag:
> (the first assumes the collection is in some space where it can be 
> grabbed by EL, the second that your custom tag does it's own OGNL 
> evaluation)
>
> <c:foreach var="customTagArg" 
> items="${some.el.expression.that.returns.the.collection}">
>   <custom:tag attr="${customTagArg}"/>
> </c:foreach>
>
> or
>
> <s:iterator value="%{model.list}">
>   <custom:tag attr="%{top}"/>
> </s:iterator>
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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


Re: [struts] parameter to custom tag

Posted by Dale Newfield <Da...@Newfield.org>.
Kropp, Henning wrote:
> I must add that s:set printed out the string as well. I am not quit sure 
> if its supposed to, but it did.

I glossed over this--I wouldn't use s:set here, but rather c:set. 
Neither one should print the value (are you sure you didn't do that for 
debugging inside your custom tag?), but since you're trying to set a 
value to be extracted with EL, I'd use the tag library that is geared 
toward EL.

That would be:
<c:set var="stringArgForCustomTag"><s:property /></c:set>
<custom:tag attr="${stringArgForCustomTag}"/>

 > My understanding of struts is not as experienced so if I may ask,
> why would I need an extra jsp to pre-evaluate the OGNL as string as you 
> proposed?

You don't.  If you want to use EL exclusively to extract the data, don't 
use s:iterator, but rather one of the c: loop tags, then you won't need 
the "set".  If you want to use OGNL to name/manipulate/etc. the data, 
then something needs to be able to take that OGNL expression and 
evaluate it.  Your current solution to that is to have s:property do the 
work, and have c:set store the results of that work in a place that can 
then be handed off to your custom tag.  An alternate solution would be 
to enable your custom tag to evaluate OGNL directly.  So you'd probably 
have within your tag a String setter for attr that just stores the 
string expression (argExpr below), then in your doTag (or doStartTag, or 
whatever) do something like the following:

     PageContext pageContext = (PageContext) getJspContext();
     ValueStack valueStack = TagUtils.getStack(pageContext);

     argValue = valueStack.findString(argExpr);

to ask OGNL to evaluate the expression for you and give you the 
resulting value.

So, two different solutions, neither of which need the :set tag:
(the first assumes the collection is in some space where it can be 
grabbed by EL, the second that your custom tag does it's own OGNL 
evaluation)

<c:foreach var="customTagArg" 
items="${some.el.expression.that.returns.the.collection}">
   <custom:tag attr="${customTagArg}"/>
</c:foreach>

or

<s:iterator value="%{model.list}">
   <custom:tag attr="%{top}"/>
</s:iterator>

-Dale

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


Re: [struts] parameter to custom tag

Posted by "Kropp, Henning" <hk...@microlution.de>.
Dale Newfield schrieb:
> Kropp, Henning wrote:
>> I figured it out. I used <s:set/ > for this. Thanks.
>>
>> <s:iterator value="model.list">
>>    <s:set name="value">
>>        <s:property />
>>    </s:set>
>>    <custom:tag attr="${value}" />                 </s:iterator>
>
> That will only work if the value you're trying to pass is a string.
> It might also add whitespace to either end of that value (due to the 
> whitespace inside your s:set tag.
>
> Why not pass the OGNL expression to your tag as a string and evaluate 
> it inside your custom tag, instead?  If you use this custom tag in 
> many places, I predict the extra jsp required to pre-evaluate the 
> OGNL->string will be error-prone...
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

I must add that s:set printed out the string as well. I am not quit sure 
if its supposed to, but it did. Therefor I even had to encapsulate 
s:property with a s:param statement. Like this

<s:iterator value="model.list">
   <s:set name="value">
         <s:param name="value">
               <s:property value="top"/>
         </s:param>
   </s:set>
   <custom:tag attr="${value}" />               
</s:iterator>

This is quite a big block for little I wanted to achieve.

I thought about passing the whole list to the tag too, but somehow it 
did not feel like the right thing to do, at least as I would have done 
it. My understanding of struts is not as experienced so if I may ask, 
why would I need an extra jsp to pre-evaluate the OGNL as string as you 
proposed?

thx

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


Re: [struts] parameter to custom tag

Posted by Dale Newfield <Da...@Newfield.org>.
Kropp, Henning wrote:
> I figured it out. I used <s:set/ > for this. Thanks.
> 
> <s:iterator value="model.list">
>    <s:set name="value">
>        <s:property />
>    </s:set>
>    <custom:tag attr="${value}" />                 </s:iterator>

That will only work if the value you're trying to pass is a string.
It might also add whitespace to either end of that value (due to the 
whitespace inside your s:set tag.

Why not pass the OGNL expression to your tag as a string and evaluate it 
inside your custom tag, instead?  If you use this custom tag in many 
places, I predict the extra jsp required to pre-evaluate the 
OGNL->string will be error-prone...

-Dale

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


Re: parameter to custom tag

Posted by "Kropp, Henning" <hk...@microlution.de>.
Hi,

I figured it out. I used <s:set/ > for this. Thanks.

<s:iterator value="model.list">
    <s:set name="value">
        <s:property />
    </s:set>
    <custom:tag attr="${value}" />                 
</s:iterator>

regards


Kropp, Henning schrieb:
> Hi List,
>
> I am new to struts2. I iterate over a list of strings. Every string in 
> that list is supposed to be executed by a custom tag. I tried it in 
> many fashions but its not working. I am only able to give over the 
> whole list as a string with:
>
> <custom:tag attr="${model.list}" />
>
> How to I get the <s:proptery /> value into my custom tag?
>
> Kind regards
> Henning Kropp
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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