You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurie Harper <la...@holoweb.net> on 2007/01/30 01:02:59 UTC

[s2] s:select and non-primitive values

I suspect this may be related to the recently mentioned bug/limitation 
in s:select, but I haven't been able to figure out how to work around it 
if so. I'm trying to create a select input for a model property which 
has a POJO type (actually, a persistent entity). Here's a rough outline:

public class Region {
   public Integer getId() {...}
   public String getName() {...}
}

public class Player {
   public Region getRegion() {...}
   public void setRegion(Region region) {...}
}

public class SomeAction {
   public Object getModel() {/*returns Player*/}
   public List<Region> getRegions() {...}
}

<s:select name="region" list="regions"
           listKey="id" listValue="name"
           headerKey="-1" headerValue=""
           label="Region:" labelposition="left"/>

This works well except for selection of the current value. The select 
input always shows with the default/empty option selected. Making 
Region.getId() return 'int' instead of Integer doesn't seem to help, and 
I'm not able to find any other combination of changes that does work.

I should point out that I also have a converter registered globally for 
the Region type. It only ever gets called on form submission, though, to 
convert the selected value from String id to a Region instance. It's 
never called to convert from Region to String.

Am I doing something fundamentally wrong? The selected value *is* set on 
the Player model instance, so everything seems to be working fine except 
for the display of the current property value in the form.

L.


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


Re: [s2] s:select and non-primitive values

Posted by Don Brown <do...@gmail.com>.
This seems related to https://issues.apache.org/struts/browse/WW-1601
and http://issues.apache.org/struts/browse/WW-1634

Don

On 1/29/07, Laurie Harper <la...@holoweb.net> wrote:
> I suspect this may be related to the recently mentioned bug/limitation
> in s:select, but I haven't been able to figure out how to work around it
> if so. I'm trying to create a select input for a model property which
> has a POJO type (actually, a persistent entity). Here's a rough outline:
>
> public class Region {
>    public Integer getId() {...}
>    public String getName() {...}
> }
>
> public class Player {
>    public Region getRegion() {...}
>    public void setRegion(Region region) {...}
> }
>
> public class SomeAction {
>    public Object getModel() {/*returns Player*/}
>    public List<Region> getRegions() {...}
> }
>
> <s:select name="region" list="regions"
>            listKey="id" listValue="name"
>            headerKey="-1" headerValue=""
>            label="Region:" labelposition="left"/>
>
> This works well except for selection of the current value. The select
> input always shows with the default/empty option selected. Making
> Region.getId() return 'int' instead of Integer doesn't seem to help, and
> I'm not able to find any other combination of changes that does work.
>
> I should point out that I also have a converter registered globally for
> the Region type. It only ever gets called on form submission, though, to
> convert the selected value from String id to a Region instance. It's
> never called to convert from Region to String.
>
> Am I doing something fundamentally wrong? The selected value *is* set on
> the Player model instance, so everything seems to be working fine except
> for the display of the current property value in the form.
>
> L.
>
>
> ---------------------------------------------------------------------
> 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: [s2] s:select and non-primitive values

Posted by Laurie Harper <la...@holoweb.net>.
Stuart Piltch wrote:
> Laurie Harper <laurie <at> holoweb.net> writes:
>> <s:select name="region" list="regions"
>>            listKey="id" listValue="name"
>>            headerKey="-1" headerValue=""
>>            label="Region:" labelposition="left"/>
> 
> Hi Laurie,
> 
> I ran into a similar problem a few days ago. I posted my workaround here:
> 
> http://article.gmane.org/gmane.comp.jakarta.struts.user/134624
> 
> Since then, I've been using FreeMarker for some of the results. That ends up
> giving one more workaround:
> 
> Using your example (ignoring the Region converter for now), this would work in
> FreeMarker:
> 
> <@s.select name="regionId" list="regions" listKey="id" listValue="name" 
>            headerKey="-1" headerValue=""
>            label="Region:" labelposition="left" value="regionId.toString()" />
> 
> If you wanted to keep this workaround in the JSP itself, without using the
> Integer/String workaround in the model that I mentioned above, you'd have to do
> something like this:
> 
> <s:select name="regionId" list="regions" listKey="id" listValue="name" 
>            headerKey="-1" headerValue=""
>            label="Region:" labelposition="left" 
>            value="regionId == null ? '-1' : regionId.toString()" />
> 
> A bit ugly, but it should do the job until the bugs are fixed.
> 
> Cheers,
> 
>  - stuart

Stuart, nice workaround! :-) I added a comment to the JIRA ticket.

L.


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


Re: [s2] s:select and non-primitive values

Posted by Stuart Piltch <st...@gradientblue.com>.
Laurie Harper <laurie <at> holoweb.net> writes:
> <s:select name="region" list="regions"
>            listKey="id" listValue="name"
>            headerKey="-1" headerValue=""
>            label="Region:" labelposition="left"/>

Hi Laurie,

I ran into a similar problem a few days ago. I posted my workaround here:

http://article.gmane.org/gmane.comp.jakarta.struts.user/134624

Since then, I've been using FreeMarker for some of the results. That ends up
giving one more workaround:

Using your example (ignoring the Region converter for now), this would work in
FreeMarker:

<@s.select name="regionId" list="regions" listKey="id" listValue="name" 
           headerKey="-1" headerValue=""
           label="Region:" labelposition="left" value="regionId.toString()" />

If you wanted to keep this workaround in the JSP itself, without using the
Integer/String workaround in the model that I mentioned above, you'd have to do
something like this:

<s:select name="regionId" list="regions" listKey="id" listValue="name" 
           headerKey="-1" headerValue=""
           label="Region:" labelposition="left" 
           value="regionId == null ? '-1' : regionId.toString()" />

A bit ugly, but it should do the job until the bugs are fixed.

Cheers,

 - stuart


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