You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alex Siman <al...@gmail.com> on 2009/10/12 23:57:39 UTC

How to get value of tag attribute from OGNL?

I have created JSP file tag as it is described here  
http://java.sun.com/javaee/5/docs/tutorial/doc/bnama.html

To get value of tag attribute 'someTagAttribute' I use this code:
<s:property value="%{#attr.someTagAttribute}"/>

OGNL context works fine within this file tag. But I realized that if there
will be an action property with the same name 'someTagAttribute' and value
for tag attribute will not be set, then the above code will return value of
action property as if it is the value of tag attribute.

How to get value of tag attribute directly from OGNL?
-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25862967.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


[CLOSED] Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
Seems like question is closed now.

Thanks.

Alex Siman wrote:
> 
> Thanx you Dave for your advice! 
> 
> I switched off the JSTL-EL (as it was recomended in Struts2 wiki), so I
> wrote another working code:
> 
> 	<% request.setAttribute("jspContext", jspContext); %>
> 	<s:property value="%{#attr.jspContext.getAttribute('fieldName')}"/>
> 
> 
> And another more complex variant:
> --------------------------------------
> 	<% 
> 		class JspContextUtils {
> 			private static final String ATTR_MAP_NAME = "jspContext";
> 			
> 			private PageContext pageContext;
> 			private ServletRequest request;
> 			
> 			public JspContextUtils(JspContext jspContext) {
> 				pageContext = (PageContext) jspContext;
> 				request = pageContext.getRequest();
> 			}
> 			
> 			public void exposeTagAttributeToOgnl(String attrName) {
> 				Map<String, Object> attrMap = null;
> 				Object attrMapObj = request.getAttribute(ATTR_MAP_NAME);
> 				if (attrMapObj == null || !(attrMapObj instanceof Map)) {
> 					attrMap = new HashMap<String, Object>();
> 					request.setAttribute(ATTR_MAP_NAME, attrMap);
> 				} else {
> 					attrMap = (Map<String, Object>) attrMapObj;
> 				}
> 				attrMap.put(attrName, pageContext.getAttribute(attrName));
> 			}
> 		}
> 
> 		JspContextUtils jspContextUtils = new JspContextUtils(jspContext);
> 	%>
> 
> 	<% jspContextUtils.exposeTagAttributeToOgnl("fieldName"); %>
> 	<s:property value="%{#attr.jspContext.fieldName}"/>
> --------------------------------------
> 
> 
> P.S. What did you mean by this?:
> 
> 	"The answer is easier if this .tag file contains no recursive calls."
> 
> 
> DNewfield wrote:
>> 
>> The answer is easier if this .tag file contains no recursive calls.
>> The issue is that the .tag file receives attributes through jstl-el, not 
>> ognl.  You can use the jstl-el c:set tag to promote that value into a 
>> namespace that's also accessible from ognl.
>> 
>> Alex Siman wrote:
>>> /WEB-INF/tags/formField.tag
>>> --------------------------------------------------------------
>>> <%@ attribute name="fieldName" %>
>> <c:set var="fieldNameInTag" scope="request" value="${fieldName}"/>
>> <s:property value="%{#request.fieldNameInTag}"/>
>> 
>> -Dale
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25876485.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Musachy Barroso wrote:
> hum, yeah after rtexprvalue=false it should work fine, please edit the wiki :)

Done.

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
hum, yeah after rtexprvalue=false it should work fine, please edit the wiki :)

musachy

On Tue, Oct 13, 2009 at 12:42 PM, Dale Newfield <da...@newfield.org> wrote:
> Musachy Barroso wrote:
>>
>> that's only a problem for defining maps in ognl, which used the
>> "#{a:b}" syntax, which made the jstl el complain. The solution is just
>> to use the alternative map syntax:
>>
>> #@java.util.LinkedHashMap@{ "foo" : "foo value", "bar" : "bar value" }
>>
>>
>> http://www.opensymphony.com/ognl/html/LanguageGuide/collectionConstruction.html#mapConstruction
>
> I have places in my current codebase where I use the #{a:b} syntax without
> issue.  Does jstl-el not complain because I added those since the
> rtexprvalue=false change?  If so, anyone mind if I remove that portion of
> the wiki page (replacing it with a link to the ognl mapContruction link)?
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Musachy Barroso wrote:
> that's only a problem for defining maps in ognl, which used the
> "#{a:b}" syntax, which made the jstl el complain. The solution is just
> to use the alternative map syntax:
> 
> #@java.util.LinkedHashMap@{ "foo" : "foo value", "bar" : "bar value" }
> 
> http://www.opensymphony.com/ognl/html/LanguageGuide/collectionConstruction.html#mapConstruction

I have places in my current codebase where I use the #{a:b} syntax 
without issue.  Does jstl-el not complain because I added those since 
the rtexprvalue=false change?  If so, anyone mind if I remove that 
portion of the wiki page (replacing it with a link to the ognl 
mapContruction link)?

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
that's only a problem for defining maps in ognl, which used the
"#{a:b}" syntax, which made the jstl el complain. The solution is just
to use the alternative map syntax:

#@java.util.LinkedHashMap@{ "foo" : "foo value", "bar" : "bar value" }

http://www.opensymphony.com/ognl/html/LanguageGuide/collectionConstruction.html#mapConstruction

On Tue, Oct 13, 2009 at 12:27 PM, Dale Newfield <da...@newfield.org> wrote:
> Alex Siman wrote:
>>>>
>>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>>>
>>> Can you please point me to that?  Seems quite strange to me...
>>
>> Read the section "JSP 2.1" here:
>> http://struts.apache.org/2.1.8/docs/ognl.html
>
> That was added by Ted Husted on 3/23/2007.
>
> What are the problems that this addresses?  It seems that the cure is worse
> than the disease of having to escape a few #'s here and there (none in my
> codebase), especially after we've since restricted all the struts tags with
> rtexprvalue=false...
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
that's only a problem for defining maps in ognl, which used the
"#{a:b}" syntax, which made the jstl el complain. The solution is just
to use the alternative map syntax:

#@java.util.LinkedHashMap@{ "foo" : "foo value", "bar" : "bar value" }

http://www.opensymphony.com/ognl/html/LanguageGuide/collectionConstruction.html#mapConstruction

On Tue, Oct 13, 2009 at 12:27 PM, Dale Newfield <da...@newfield.org> wrote:
> Alex Siman wrote:
>>>>
>>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>>>
>>> Can you please point me to that?  Seems quite strange to me...
>>
>> Read the section "JSP 2.1" here:
>> http://struts.apache.org/2.1.8/docs/ognl.html
>
> That was added by Ted Husted on 3/23/2007.
>
> What are the problems that this addresses?  It seems that the cure is worse
> than the disease of having to escape a few #'s here and there (none in my
> codebase), especially after we've since restricted all the struts tags with
> rtexprvalue=false...
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Alex Siman wrote:
>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>> Can you please point me to that?  Seems quite strange to me...
> 
> Read the section "JSP 2.1" here: 
> http://struts.apache.org/2.1.8/docs/ognl.html

That was added by Ted Husted on 3/23/2007.

What are the problems that this addresses?  It seems that the cure is 
worse than the disease of having to escape a few #'s here and there 
(none in my codebase), especially after we've since restricted all the 
struts tags with rtexprvalue=false...

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
Cool, welcome back, JSTL-EL!

DNewfield wrote:
> 
> Alex Siman wrote:
>>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>>> Can you please point me to that?  Seems quite strange to me...
> 
> Since we've removed the possibility of including JSTL-EL expressions in 
> struts tag attributes, this advice is no longer appropriate.  If that's 
> the reason you chose to write huge scriptlets instead of small jstl tag 
> calls, consider the advice rescinded.
> 
>> Read the section "JSP 2.1" here: 
>> http://struts.apache.org/2.1.8/docs/ognl.html
> 
> I have attempted to modify these to address this issue, but since the 
> changes actually reside at 
> http://cwiki.apache.org/confluence/display/WW/OGNL I'm not sure when/if 
> those will propagate.
> 
>>> "The answer is easier if this .tag file contains no recursive calls."
>> 
>> I think this issues can be put back until .tag file calls itself, right?
> 
> Or until it calls another tag that also calls the original, correct.
> 
>> P.S. Dale, sorry for 'Dave')) Kinda 'Dale Newfield' ~= 'Dave Newton'.
> 
> No big deal -- you're not the first to make that same mistake :-).  I'm 
> honored to be confused with someone whose advice I respect!  On the 
> other hand I should apologize right now for having in the past confused 
> our two resident Martin's, who (IMHO) don't deserve equal respect!
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25880814.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Alex Siman wrote:
>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>> Can you please point me to that?  Seems quite strange to me...

Since we've removed the possibility of including JSTL-EL expressions in 
struts tag attributes, this advice is no longer appropriate.  If that's 
the reason you chose to write huge scriptlets instead of small jstl tag 
calls, consider the advice rescinded.

> Read the section "JSP 2.1" here: 
> http://struts.apache.org/2.1.8/docs/ognl.html

I have attempted to modify these to address this issue, but since the 
changes actually reside at 
http://cwiki.apache.org/confluence/display/WW/OGNL I'm not sure when/if 
those will propagate.

>> "The answer is easier if this .tag file contains no recursive calls."
> 
> I think this issues can be put back until .tag file calls itself, right?

Or until it calls another tag that also calls the original, correct.

> P.S. Dale, sorry for 'Dave')) Kinda 'Dale Newfield' ~= 'Dave Newton'.

No big deal -- you're not the first to make that same mistake :-).  I'm 
honored to be confused with someone whose advice I respect!  On the 
other hand I should apologize right now for having in the past confused 
our two resident Martin's, who (IMHO) don't deserve equal respect!

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Alex Siman wrote:
>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>> Can you please point me to that?  Seems quite strange to me...
> 
> Read the section "JSP 2.1" here: 
> http://struts.apache.org/2.1.8/docs/ognl.html

That was added by Ted Husted on 3/23/2007.

What are the problems that this addresses?  It seems that the cure is 
worse than the disease of having to escape a few #'s here and there 
(none in my codebase), especially after we've since restricted all the 
struts tags with rtexprvalue=false...

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>
> Can you please point me to that?  Seems quite strange to me...

Read the section "JSP 2.1" here: 
http://struts.apache.org/2.1.8/docs/ognl.html

> "The answer is easier if this .tag file contains no recursive calls."

I think this issues can be put back until .tag file calls itself, right?

P.S. Dale, sorry for 'Dave')) Kinda 'Dale Newfield' ~= 'Dave Newton'.
-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25877323.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
trust me, that is nothing compared to what I have seen in scriptlets.
That expression could be broken down into an "if" block, even the
original is not that hard to read. Assuming you don't put any business
logic in scriplets, which is evil by definition (an axiom I guess),
pages containing scriptlets are very hard to read, maintain, and of
course debug. Even reading your own scriptlets will be hard 6 months
after you write them. Whenever you think you need an scriptlet, double
check, there is a very high chance that you have other, cleaner, ways
of doing the same thing.

that's of course my very own personal opinion :)

musachy

On Tue, Oct 13, 2009 at 10:48 AM, Alex Siman <al...@gmail.com> wrote:
>
> Why are scriptlets an evil? Is it relates to an architectural aspect that
> View (JSP) must contain no business logic? Or something else? BTW sometimes
> OGNL also contains little complex code. What about this one:
>
>        <s:set var="label" value="%{#attr.labelKey != null ?
> getText(#attr.labelKey) : (#attr.label != null ? #attr.label : '')}"/>
>
> It is not so kind to deal w/ code like that((. But what to do? Add one more
> function to *Action class?
>
> Musachy Barroso wrote:
>>
>> on top of that, you can use the the struts "set" tag to set stuff in
>> any of the contexts. Scriptlets are evil.
>>
>> musachy
>>
>> On Tue, Oct 13, 2009 at 9:41 AM, Dale Newfield <da...@newfield.org> wrote:
>>> Alex Siman wrote:
>>>>
>>>> Thanx you Dave for your advice!
>>>
>>> Sure, Alix.
>>>
>>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>>>
>>> Can you please point me to that?  Seems quite strange to me...
>>>
>>>> so I wrote another working code:
>>>
>>> snippets of java inside your .jsp seems like a step back about 8 years to
>>> me...
>>>
>>>> P.S. What did you mean by this?:
>>>>
>>>>        "The answer is easier if this .tag file contains no recursive
>>>> calls."
>>>
>>> If the location you're stashing the value in order to get to it from OGNL
>>> is
>>> not related to the call structure, then recursive calls will overwrite
>>> the
>>> values set by the parent.  My example shoved the values into the request.
>>>  Yours is putting them in the jspContext, but I believe that's also a
>>> shared
>>> jspContext for all the code that's generating that specific output.
>>>
>>> If you call <myTagDir:myTag />, within it set a passed value in a larger
>>> scope, then make a recursive call also to <myTagDir:myTag />, when that
>>> recursive call returns that larger scope will still contain the value
>>> passed
>>> into the the recursive call and *not* the value passed into the initial
>>> tag
>>> (and promoted to that larger scope location prior to the recursive call).
>>>
>>> -Dale
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>
>>
>>
>> --
>> "Hey you! Would you help me to carry the stone?" Pink Floyd
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25877596.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
Why are scriptlets an evil? Is it relates to an architectural aspect that
View (JSP) must contain no business logic? Or something else? BTW sometimes
OGNL also contains little complex code. What about this one:

	<s:set var="label" value="%{#attr.labelKey != null ?
getText(#attr.labelKey) : (#attr.label != null ? #attr.label : '')}"/>

It is not so kind to deal w/ code like that((. But what to do? Add one more
function to *Action class?

Musachy Barroso wrote:
> 
> on top of that, you can use the the struts "set" tag to set stuff in
> any of the contexts. Scriptlets are evil.
> 
> musachy
> 
> On Tue, Oct 13, 2009 at 9:41 AM, Dale Newfield <da...@newfield.org> wrote:
>> Alex Siman wrote:
>>>
>>> Thanx you Dave for your advice!
>>
>> Sure, Alix.
>>
>>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>>
>> Can you please point me to that?  Seems quite strange to me...
>>
>>> so I wrote another working code:
>>
>> snippets of java inside your .jsp seems like a step back about 8 years to
>> me...
>>
>>> P.S. What did you mean by this?:
>>>
>>>        "The answer is easier if this .tag file contains no recursive
>>> calls."
>>
>> If the location you're stashing the value in order to get to it from OGNL
>> is
>> not related to the call structure, then recursive calls will overwrite
>> the
>> values set by the parent.  My example shoved the values into the request.
>>  Yours is putting them in the jspContext, but I believe that's also a
>> shared
>> jspContext for all the code that's generating that specific output.
>>
>> If you call <myTagDir:myTag />, within it set a passed value in a larger
>> scope, then make a recursive call also to <myTagDir:myTag />, when that
>> recursive call returns that larger scope will still contain the value
>> passed
>> into the the recursive call and *not* the value passed into the initial
>> tag
>> (and promoted to that larger scope location prior to the recursive call).
>>
>> -Dale
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 
> 
> -- 
> "Hey you! Would you help me to carry the stone?" Pink Floyd
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25877596.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
on top of that, you can use the the struts "set" tag to set stuff in
any of the contexts. Scriptlets are evil.

musachy

On Tue, Oct 13, 2009 at 9:41 AM, Dale Newfield <da...@newfield.org> wrote:
> Alex Siman wrote:
>>
>> Thanx you Dave for your advice!
>
> Sure, Alix.
>
>> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)
>
> Can you please point me to that?  Seems quite strange to me...
>
>> so I wrote another working code:
>
> snippets of java inside your .jsp seems like a step back about 8 years to
> me...
>
>> P.S. What did you mean by this?:
>>
>>        "The answer is easier if this .tag file contains no recursive
>> calls."
>
> If the location you're stashing the value in order to get to it from OGNL is
> not related to the call structure, then recursive calls will overwrite the
> values set by the parent.  My example shoved the values into the request.
>  Yours is putting them in the jspContext, but I believe that's also a shared
> jspContext for all the code that's generating that specific output.
>
> If you call <myTagDir:myTag />, within it set a passed value in a larger
> scope, then make a recursive call also to <myTagDir:myTag />, when that
> recursive call returns that larger scope will still contain the value passed
> into the the recursive call and *not* the value passed into the initial tag
> (and promoted to that larger scope location prior to the recursive call).
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
Alex Siman wrote:
> Thanx you Dave for your advice! 

Sure, Alix.

> I switched off the JSTL-EL (as it was recomended in Struts2 wiki)

Can you please point me to that?  Seems quite strange to me...

> so I wrote another working code:

snippets of java inside your .jsp seems like a step back about 8 years 
to me...

> P.S. What did you mean by this?:
> 
> 	"The answer is easier if this .tag file contains no recursive calls."

If the location you're stashing the value in order to get to it from 
OGNL is not related to the call structure, then recursive calls will 
overwrite the values set by the parent.  My example shoved the values 
into the request.  Yours is putting them in the jspContext, but I 
believe that's also a shared jspContext for all the code that's 
generating that specific output.

If you call <myTagDir:myTag />, within it set a passed value in a larger 
scope, then make a recursive call also to <myTagDir:myTag />, when that 
recursive call returns that larger scope will still contain the value 
passed into the the recursive call and *not* the value passed into the 
initial tag (and promoted to that larger scope location prior to the 
recursive call).

-Dale

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


Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
Thanx you Dave for your advice! 

I switched off the JSTL-EL (as it was recomended in Struts2 wiki), so I
wrote another working code:

	<% request.setAttribute("jspContext", jspContext); %>
	<s:property value="%{#attr.jspContext.getAttribute('fieldName')}"/>


And another more complex variant:
--------------------------------------
	<% 
		class JspContextUtils {
			private static final String ATTR_MAP_NAME = "jspContext";
			
			private PageContext pageContext;
			private ServletRequest request;
			
			public JspContextUtils(JspContext jspContext) {
				pageContext = (PageContext) jspContext;
				request = pageContext.getRequest();
			}
			
			public void exposeTagAttributeToOgnl(String attrName) {
				Map<String, Object> attrMap = null;
				Object attrMapObj = request.getAttribute(ATTR_MAP_NAME);
				if (attrMapObj == null || !(attrMapObj instanceof Map)) {
					attrMap = new HashMap<String, Object>();
					request.setAttribute(ATTR_MAP_NAME, attrMap);
				} else {
					attrMap = (Map<String, Object>) attrMapObj;
				}
				attrMap.put(attrName, pageContext.getAttribute(attrName));
			}
		}

		JspContextUtils jspContextUtils = new JspContextUtils(jspContext);
	%>

	<% jspContextUtils.exposeTagAttributeToOgnl("fieldName"); %>
	<s:property value="%{#attr.jspContext.fieldName}"/>
--------------------------------------


P.S. What did you mean by this?:

	"The answer is easier if this .tag file contains no recursive calls."


DNewfield wrote:
> 
> The answer is easier if this .tag file contains no recursive calls.
> The issue is that the .tag file receives attributes through jstl-el, not 
> ognl.  You can use the jstl-el c:set tag to promote that value into a 
> namespace that's also accessible from ognl.
> 
> Alex Siman wrote:
>> /WEB-INF/tags/formField.tag
>> --------------------------------------------------------------
>> <%@ attribute name="fieldName" %>
> <c:set var="fieldNameInTag" scope="request" value="${fieldName}"/>
> <s:property value="%{#request.fieldNameInTag}"/>
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25876315.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Dale Newfield <da...@newfield.org>.
The answer is easier if this .tag file contains no recursive calls.
The issue is that the .tag file receives attributes through jstl-el, not 
ognl.  You can use the jstl-el c:set tag to promote that value into a 
namespace that's also accessible from ognl.

Alex Siman wrote:
> /WEB-INF/tags/formField.tag
> --------------------------------------------------------------
> <%@ attribute name="fieldName" %>
<c:set var="fieldNameInTag" scope="request" value="${fieldName}"/>
<s:property value="%{#request.fieldNameInTag}"/>

-Dale

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


RE: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
Did you mean this variant?:

<s:set var="ognlFieldName" value='%{\"<%=
jspContext.getAttribute("fieldName", PageContext.PAGE_SCOPE) %>\"}'/>

This code does NOT work. So the question still open.

Mike Baranski-2 wrote:
> 
> Why not use this?
> http://struts.apache.org/2.1.6/docs/set.html
> 
>>-----Original Message-----
>>From: Alex Siman [mailto:aleksandr.siman@gmail.com]
>>Sent: Tuesday, October 13, 2009 9:25 AM
>>To: user@struts.apache.org
>>Subject: Re: How to get value of tag attribute from OGNL?
>>
>>
>>OK, I figured out how to get value of tag attribute in JSP:
>>
>>	<%= jspContext.getAttribute("fieldName", PageContext.PAGE_SCOPE)
>>%>
>>
>>Now I need to put this value into some OGNL variable, say
>>'#ognlFieldName',
>>to get access to this value from Struts2 tags.
>>
>>So, how to get access to jspContext from OGNL or at least Java code?
>>--
>>View this message in context: http://www.nabble.com/How-to-get-value-of-
>>tag-attribute-from-OGNL--tp25862967p25872856.html
>>Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>>---------------------------------------------------------------------
>>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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25873140.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: How to get value of tag attribute from OGNL?

Posted by Mike Baranski <li...@secmgmt.com>.
Why not use this?
http://struts.apache.org/2.1.6/docs/set.html

>-----Original Message-----
>From: Alex Siman [mailto:aleksandr.siman@gmail.com]
>Sent: Tuesday, October 13, 2009 9:25 AM
>To: user@struts.apache.org
>Subject: Re: How to get value of tag attribute from OGNL?
>
>
>OK, I figured out how to get value of tag attribute in JSP:
>
>	<%= jspContext.getAttribute("fieldName", PageContext.PAGE_SCOPE)
>%>
>
>Now I need to put this value into some OGNL variable, say
>'#ognlFieldName',
>to get access to this value from Struts2 tags.
>
>So, how to get access to jspContext from OGNL or at least Java code?
>--
>View this message in context: http://www.nabble.com/How-to-get-value-of-
>tag-attribute-from-OGNL--tp25862967p25872856.html
>Sent from the Struts - User mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>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: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
OK, I figured out how to get value of tag attribute in JSP:

	<%= jspContext.getAttribute("fieldName", PageContext.PAGE_SCOPE) %>

Now I need to put this value into some OGNL variable, say '#ognlFieldName',
to get access to this value from Struts2 tags. 

So, how to get access to jspContext from OGNL or at least Java code?
-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25872856.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Alex Siman <al...@gmail.com>.
I have such custom JSP tag file:

/WEB-INF/tags/formField.tag
--------------------------------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ tag language="java" pageEncoding="UTF-8" %>

<%@ attribute name="fieldName" %>
<%@ attribute name="inputId" %>
<%@ attribute name="label" %>
<%@ attribute name="labelKey" %>
<%@ attribute name="required" %>

<s:set var="inputId" value="%{#attr.inputId != null ? #attr.inputId :
(#attr.fieldName != null ? #attr.fieldName : '')}"/>
<s:set var="label" value="%{#attr.labelKey != null ? getText(#attr.labelKey)
: (#attr.label != null ? #attr.label : '')}"/>
<s:set var="fieldErrors" value="%{fieldErrors[#attr.fieldName]}"/>
<s:set var="hasFieldErrors" value="%{#fieldErrors != null &&
#fieldErrors.size > 0}"/>

<div class="wwgrp <s:property value="%{#hasFieldErrors ? 'HasFieldErrors' :
''}"/>">
	<table class="HiddenTable">
		<tr>
			<td class="wwlbl">
				<s:if test="%{!isBlank(#label)}">
					<label class="label" for="<s:property value="%{#inputId}"/>">
						<s:if test="%{#attr.required}">*</s:if>
						<s:property value="%{#label}" escape="false"/>:</label>
				</s:if>
			</td>
			<td class="wwctrl">
				<jsp:doBody/>
				<s:if test="%{#hasFieldErrors}">
					<ul class="wwerr">
						<s:iterator var="error" value="%{#fieldErrors}">
							<li class="errorMessage">
								<s:property value="%{#error}" escape="false"/></li>
						</s:iterator>
					</ul>
				</s:if>
			 </td>
		 </tr>
	</table>
</div>
--------------------------------------------------------------

And here is the part of my "login.jsp" that uses this custom tag:
--------------------------------------------------------------
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
...
<s:form action="login">
	...
	<t:formField fieldName="password" required="true"
labelKey="msgkey.label.password">
		<input type="password" id="password" name="password"/>
		TODO: Forgot your password?
	</t:formField>
	...
</s:form>
--------------------------------------------------------------

Suppose action has a property 'fieldName' and I need to get value of a tag
attribute w/ the same name. I get tag atribute using <s:property
value="%{#attr.fieldName}"/>. The issue here is: if jsp page that uses this
custom tag does not provide value for 'fieldName' attribute, then
<s:property value="%{#attr.fieldName}"/> will return value of action
property 'fieldName'.

I did not found where jsp tag stored its attribute, but it seemed like in
PageContext. 
I tried to get PageContext, but ActionContext returns 'null'. Here is my
Java code:

	PageContext pageContext = ServletActionContext.getPageContext();
	if (pageContext != null)
	{
		log.debug("pageContext.findAttribute('fieldName'): " +
			pageContext.findAttribute("fieldName"));
	}


Musachy Barroso wrote:
> 
> I am not sure I understand what you mean. Is "someTagAttribute" set in
> the context by your custom tag?
> 
> musachy
> 
> On Mon, Oct 12, 2009 at 2:57 PM, Alex Siman <al...@gmail.com>
> wrote:
>>
>> I have created JSP file tag as it is described here
>> http://java.sun.com/javaee/5/docs/tutorial/doc/bnama.html
>>
>> To get value of tag attribute 'someTagAttribute' I use this code:
>> <s:property value="%{#attr.someTagAttribute}"/>
>>
>> OGNL context works fine within this file tag. But I realized that if
>> there
>> will be an action property with the same name 'someTagAttribute' and
>> value
>> for tag attribute will not be set, then the above code will return value
>> of
>> action property as if it is the value of tag attribute.
>>
>> How to get value of tag attribute directly from OGNL?
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25862967.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 
> 
> -- 
> "Hey you! Would you help me to carry the stone?" Pink Floyd
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25871222.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to get value of tag attribute from OGNL?

Posted by Musachy Barroso <mu...@gmail.com>.
I am not sure I understand what you mean. Is "someTagAttribute" set in
the context by your custom tag?

musachy

On Mon, Oct 12, 2009 at 2:57 PM, Alex Siman <al...@gmail.com> wrote:
>
> I have created JSP file tag as it is described here
> http://java.sun.com/javaee/5/docs/tutorial/doc/bnama.html
>
> To get value of tag attribute 'someTagAttribute' I use this code:
> <s:property value="%{#attr.someTagAttribute}"/>
>
> OGNL context works fine within this file tag. But I realized that if there
> will be an action property with the same name 'someTagAttribute' and value
> for tag attribute will not be set, then the above code will return value of
> action property as if it is the value of tag attribute.
>
> How to get value of tag attribute directly from OGNL?
> --
> View this message in context: http://www.nabble.com/How-to-get-value-of-tag-attribute-from-OGNL--tp25862967p25862967.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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