You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by oscar perez <os...@gmail.com> on 2008/03/03 22:59:42 UTC

Problem with OGNL syntax

Hi!
I  am having a very strange problem with struts 2. I am newbie to Struts 2
so maybe it is a simple thing I misunderstood.
I have an abstract class (enterprise) and 4 subclasses. I want to perform
CRUD operations on them and since all the properties are common to all the
subclasses I've decided to use only one form and one action for all of them.
The problem is that I cannot use in the form the reference to the abstract
class Enterprise since when I create a new entry the bean instantiation
fails as Enterprise is an abstract class and I didnt want to create 4 forms
either so I came up with the following

In the form page to get the type of the company that I am editing and using
logic tags to create a variable holding the name of the proper instance..:

<s:if test="typeCompany.equalsIgnoreCase('CUSTOMER')">
<s:set name="companyType" value="%{'customer'}/>
....

and then I try to use the proper name in the form tags like that:

<s:textfield name="%{companyType}.name" value="%{%{companyType}.name}"/>

and this shows nothing as value
If instead I use the following

<s:textfield name="%{companyType}.name" value="%{companyType.name}"/>
I get the string customer.name as value
But now if I use the key field like this:
<s:textfield key="%{companyType.name}" name="%{companyType}.name" />

Then it works just fine, It renders the value that customer.name holds. I
would like to use value rather than key because I don't want to create more
resource bundles for localization since enterprise.name suits to the 4
subclasses.

I hope I managed to explain my issue clearly. Any help is highly
appreciated.
Thank you!
Óscar

Re: Problem with OGNL syntax

Posted by oscar perez <os...@gmail.com>.
Hi,
Thank you for your quick reply. I tried your suggested syntax and didn't
work either.
However, I like a lot more your last proposal so I will give it a shot.
Thanks!
Oscar

On Tue, Mar 4, 2008 at 1:05 AM, Jeromy Evans <
jeromy.evans@blueskyminds.com.au> wrote:

> oscar perez wrote:
> >
> > <s:if test="typeCompany.equalsIgnoreCase('CUSTOMER')">
> > <s:set name="companyType" value="%{'customer'}/>
> > ....
> >
> > and then I try to use the proper name in the form tags like that:
> >
> > <s:textfield name="%{companyType}.name" value="%{%{companyType}.name}"/>
> >
> >
> I see what you're trying to do; you're asking OGNL to evaluate
> companyType first, and use the string result in the outer expression to
> evaluate.
>
> The %{} notation won't do this for you.  According to the OGNL guide you
> should use parentheses to evaluate part of the expression as a unit and
> change the evlatulation order [1]
>
> eg
>
> <s:textfield name="%{(companyType).name}" value="%{(companyType).name}"/>
>
> Haven't tried that myself.
>
> see : Parenthetical Expressions
> [1] http://www.ognl.org/basicExpressions.html
>
>
> As a side note though... I don't like your approach.  As your four
> objects share the same interface you should have just one property
> called Enterprise and your form should address that.  Your action should
> implement Preparable and instantiate the appropriate concrete
> implementation prior to the params being set.  If you use the
> params-prepare-params stack you can pass parameters to your action, then
> instantiate the appropriate concrete implementation, then set the
> parameters on the implementation.
>
> regards,
>  Jromy Evans
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Problem with OGNL syntax

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
oscar perez wrote:
>
> <s:if test="typeCompany.equalsIgnoreCase('CUSTOMER')">
> <s:set name="companyType" value="%{'customer'}/>
> ....
>
> and then I try to use the proper name in the form tags like that:
>
> <s:textfield name="%{companyType}.name" value="%{%{companyType}.name}"/>
>   
>   
I see what you're trying to do; you're asking OGNL to evaluate 
companyType first, and use the string result in the outer expression to 
evaluate.

The %{} notation won't do this for you.  According to the OGNL guide you 
should use parentheses to evaluate part of the expression as a unit and 
change the evlatulation order [1]

eg

<s:textfield name="%{(companyType).name}" value="%{(companyType).name}"/>

Haven't tried that myself.

see : Parenthetical Expressions
[1] http://www.ognl.org/basicExpressions.html


As a side note though... I don't like your approach.  As your four 
objects share the same interface you should have just one property 
called Enterprise and your form should address that.  Your action should 
implement Preparable and instantiate the appropriate concrete 
implementation prior to the params being set.  If you use the 
params-prepare-params stack you can pass parameters to your action, then 
instantiate the appropriate concrete implementation, then set the 
parameters on the implementation.

regards,
 Jromy Evans





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