You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by "Asleson, Ryan" <as...@BIWORLDWIDE.com> on 2004/09/02 15:37:12 UTC

Dynamic Expressions for Struts-EL and JSTL

Hello,

I'm using the Struts-EL and JSTL custom tag implementations in my Web app.

I want to use the c:out tag to output dynamic data.  The tricky part is I
want to dynamically build the EL expression at runtime by building up a
String that should be evaluated.  I don't want to output the String, I want
the String to be evaluated by the expression engine and then that written
out.  An example follows below.



<%
//Assume that after this line runs, expr has a value of
formBean.property.value 
//This is the expression I want evaluated
String expr = "formBean." +
((app.LayoutObject)pageContext.getAttribute("layoutObject")).getProperty();

pageContext.setAttribute("expr", expr);
%>

<c:out value="Expression Value is: ${expr}" />



The c:out tag above renders the text Expression Value is:
formBean.property.value  .

This isn't what I want -- I want it to evaluate the formBean.property.value
down to an expression, and then spit out the value of that expression
(basically, formBean.getProperty().getValue() ).

Interestingly, if I "hard code" the expression, it works:

<c:out value="${formBean.property.value}"/>

The only problem is I need to dynamically change this expression at runtime.

Can this be done?  If so, how?  I'm hoping so otherwise I'll have a mountain
of new code to write.  Also, is there a more elegant way to do what I show
in the example above, like using tags instead of scriptlets?  I don't care
if I use Struts-EL tags, JSTL tags, or both.

Thank you very much!!

This e-mail message is being sent solely for use by the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by phone or reply by e-mail, delete the original message and destroy all copies. Thank you.

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


Re: Dynamic Expressions for Struts-EL and JSTL

Posted by Helios Alonso <ha...@atg.com.uy>.
if you have a property in any bean you can write this:

myBean.theProperty

or

myBean['theProperty']

(I didn't use it but reading a tutorial it was a big surprise for me).

So, you could change this:

>String expr = "formBean." +
>((app.LayoutObject)pageContext.getAttribute("layoutObject")).getProperty();

to this:

formBean[pageContext.layoutObject.property]

(If I understood you have a layoutObject with a property saying which 
property read from formBean).

At 08:37 02/09/2004 -0500, you wrote:

>Hello,
>
>I'm using the Struts-EL and JSTL custom tag implementations in my Web app.
>
>I want to use the c:out tag to output dynamic data.  The tricky part is I
>want to dynamically build the EL expression at runtime by building up a
>String that should be evaluated.  I don't want to output the String, I want
>the String to be evaluated by the expression engine and then that written
>out.  An example follows below.
>
>
>
><%
>//Assume that after this line runs, expr has a value of
>formBean.property.value
>//This is the expression I want evaluated
>String expr = "formBean." +
>((app.LayoutObject)pageContext.getAttribute("layoutObject")).getProperty();
>
>pageContext.setAttribute("expr", expr);
>%>
>
><c:out value="Expression Value is: ${expr}" />
>
>
>
>The c:out tag above renders the text Expression Value is:
>formBean.property.value  .
>
>This isn't what I want -- I want it to evaluate the formBean.property.value
>down to an expression, and then spit out the value of that expression
>(basically, formBean.getProperty().getValue() ).
>
>Interestingly, if I "hard code" the expression, it works:
>
><c:out value="${formBean.property.value}"/>
>
>The only problem is I need to dynamically change this expression at runtime.
>
>Can this be done?  If so, how?  I'm hoping so otherwise I'll have a mountain
>of new code to write.  Also, is there a more elegant way to do what I show
>in the example above, like using tags instead of scriptlets?  I don't care
>if I use Struts-EL tags, JSTL tags, or both.
>
>Thank you very much!!
>
>This e-mail message is being sent solely for use by the intended 
>recipient(s) and may contain confidential information.  Any unauthorized 
>review, use, disclosure or distribution is prohibited.  If you are not the 
>intended recipient, please contact the sender by phone or reply by e-mail, 
>delete the original message and destroy all copies. Thank you.
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: taglibs-user-help@jakarta.apache.org



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