You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Miguel Almeida <mi...@almeida.at> on 2013/05/29 12:27:35 UTC

How to set the name parameter on a multiple s:select

Dear all,

Imagine you have the following scenario:
- a property in your action: private Entity entity;

- Entity has a List<Entity2> entity2List
- Entity2 has a Entity3 entity3

Now, you need an s:select with multiple="true" to populate entity3. This
corresponds to having a request in the form
entity.entity2List[n].entity3.id (n=0,1,2...depending on how many items
are selected).

Question: how should the name parameter of the s:select be like to
achieve this?

Me and Lukasz  have tried the following without success (meaning
entity.entity2List has is empty):
<s:select key="centre.choose" multiple="true"
name="%{entity.entity2List[].entity3.id}" list="#someList" listKey="id"
listValue="code"/>
<s:select key="centre.choose" multiple="true"
name="entity.entity2List[].entity3.id" list="#someList" listKey="id"
listValue="code"/>


Thank you for your help!

Miguel Almeida


Re: How to set the name parameter on a multiple s:select

Posted by Dale Newfield <da...@newfield.org>.
On May 30, 2013, at 5:42 AM, Miguel Almeida <mi...@almeida.at> wrote:
> That's an alternative, yes. I do tend to prefer having the model objects
> in my actions though, to keep the number of objects to a minimum - it
> makes more sense to have a List<SomeObject> objects than an Integer[]
> ids, but maybe that's personal preference.

Of course you eventually want objects, but remember this is a web application, and the only thing the browser is supplying are ids.  It's up to your action to bridge that difference.

> I'm not sure where the ambiguity is. Take the following request
> parameters:
> entity.entity2List[0].entity3.id=10
> entity.entity2List[1].entity3.id=20
> 
> There is no ambiguity in how the Object should be populated - one
> entity, which has two Entity2 on the entity2List object: the first
> entity2 has an Entity3.id=10 and the second Entity2 has an
> Entity3.id=20.

Bzzt.  That doesn't do what you want, and it requires more data than your browser is providing.  That doesn't set the entity3 object with id 10 on the first entity2, rather it assumes there already are two entity2 objects on entity, that those already have entity3 objects on the them and then it just changes the ids!  If you model he ui on the browser page differently you can get the parameters above, but the nature of a multi select is that you're getting back multiple (unordered) values each with the same request parameter name.

Sounds like what you want are multiple single selects, so that each can have a different description on the page (identifying the entity2 for which this selection is being made), and allowing each to have a distinct index in its parameter name.  You'll still have the first problem above, though--you want to load the entity3 with the provided id and set that object on the corresponding entity2.  You can probably use the params-prepare-params stack pattern to help you, but you'll still need to have the parameter name be to just to properties on the action that your prepare knows to hydrate on prepare, then the body of the action just has to wire up the objects.

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


Re: How to set the name parameter on a multiple s:select

Posted by Miguel Almeida <mi...@almeida.at>.
Thanks for the feedback Dale.
A couple of thoughts below.
On Wed, 2013-05-29 at 08:18 -0400, Dale Newfield wrote:

> I would have a setter on my action class that takes an array of ids.  Then the action does the appropriate lookups, and sets the hydrated objects on the appropriate models.

That's an alternative, yes. I do tend to prefer having the model objects
in my actions though, to keep the number of objects to a minimum - it
makes more sense to have a List<SomeObject> objects than an Integer[]
ids, but maybe that's personal preference.

> You do recognize that the data you provided is ambiguous, though, right?  Nothing you stated is sufficient to know *which* entity2 objects should be set on entity, just which entity3 objects you want those entity2 objects to have.  In fact, if you don't happen to get a number of entity3 ids that matches the number of entity2s on entity, you've left yourself no way of knowing how to proceed. (Remember, all you get is a (potentially unordered) list of ids from the form submission, not tuples.)

I'm not sure where the ambiguity is. Take the following request
parameters:
entity.entity2List[0].entity3.id=10
entity.entity2List[1].entity3.id=20

There is no ambiguity in how the Object should be populated - one
entity, which has two Entity2 on the entity2List object: the first
entity2 has an Entity3.id=10 and the second Entity2 has an
Entity3.id=20.
In fact, this request works as expected (as evidenced by the test case
[1]). The challenge is to create an s:select that generates that request
parameters!

Miguel

[1] Unit Test snippet

request.setParameter("entity.entity2List[0].entity3.id", "2");
		request.setParameter("entity.entity2List[1].entity3.id", "11");

		createAction("/example/Conversion.action");

		executeProxy();
		
		Entity entity = ((TypeConversionAction) action).getEntity();
		assertNotNull(entity);
		assertNotNull(entity.getEntity2List());
		assertEquals(2, entity.getEntity2List().size());
		
		assertEquals(2, entity.getEntity2List().get(0).getEntity3().getId());
		assertEquals(11, entity.getEntity2List().get(1).getEntity3().getId());

> 
> -Dale
> 
> On May 29, 2013, at 6:27 AM, Miguel Almeida <mi...@almeida.at> wrote:
> 
> > Dear all,
> > 
> > Imagine you have the following scenario:
> > - a property in your action: private Entity entity;
> > 
> > - Entity has a List<Entity2> entity2List
> > - Entity2 has a Entity3 entity3
> > 
> > Now, you need an s:select with multiple="true" to populate entity3. This
> > corresponds to having a request in the form
> > entity.entity2List[n].entity3.id (n=0,1,2...depending on how many items
> > are selected).
> > 
> > Question: how should the name parameter of the s:select be like to
> > achieve this?
> > 
> > Me and Lukasz  have tried the following without success (meaning
> > entity.entity2List has is empty):
> > <s:select key="centre.choose" multiple="true"
> > name="%{entity.entity2List[].entity3.id}" list="#someList" listKey="id"
> > listValue="code"/>
> > <s:select key="centre.choose" multiple="true"
> > name="entity.entity2List[].entity3.id" list="#someList" listKey="id"
> > listValue="code"/>
> > 
> > 
> > Thank you for your help!
> > 
> > Miguel Almeida
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 



Re: How to set the name parameter on a multiple s:select

Posted by Dale Newfield <da...@newfield.org>.
I would have a setter on my action class that takes an array of ids.  Then the action does the appropriate lookups, and sets the hydrated objects on the appropriate models.
You do recognize that the data you provided is ambiguous, though, right?  Nothing you stated is sufficient to know *which* entity2 objects should be set on entity, just which entity3 objects you want those entity2 objects to have.  In fact, if you don't happen to get a number of entity3 ids that matches the number of entity2s on entity, you've left yourself no way of knowing how to proceed. (Remember, all you get is a (potentially unordered) list of ids from the form submission, not tuples.)

-Dale

On May 29, 2013, at 6:27 AM, Miguel Almeida <mi...@almeida.at> wrote:

> Dear all,
> 
> Imagine you have the following scenario:
> - a property in your action: private Entity entity;
> 
> - Entity has a List<Entity2> entity2List
> - Entity2 has a Entity3 entity3
> 
> Now, you need an s:select with multiple="true" to populate entity3. This
> corresponds to having a request in the form
> entity.entity2List[n].entity3.id (n=0,1,2...depending on how many items
> are selected).
> 
> Question: how should the name parameter of the s:select be like to
> achieve this?
> 
> Me and Lukasz  have tried the following without success (meaning
> entity.entity2List has is empty):
> <s:select key="centre.choose" multiple="true"
> name="%{entity.entity2List[].entity3.id}" list="#someList" listKey="id"
> listValue="code"/>
> <s:select key="centre.choose" multiple="true"
> name="entity.entity2List[].entity3.id" list="#someList" listKey="id"
> listValue="code"/>
> 
> 
> Thank you for your help!
> 
> Miguel Almeida
> 

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


Re: How to set the name parameter on a multiple s:select

Posted by Miguel Almeida <mi...@almeida.at>.
I've also posted this on
http://stackoverflow.com/questions/16812357/how-to-set-the-name-parameter-on-a-multiple-sselect so the knowledge can be shared once the answer is known.
I also plan to add this to struts' documentation!

Miguel

On Wed, 2013-05-29 at 11:27 +0100, Miguel Almeida wrote:

> Imagine you have the following scenario:
> - a property in your action: private Entity entity;
> 
> - Entity has a List<Entity2> entity2List
> - Entity2 has a Entity3 entity3
> 
> Now, you need an s:select with multiple="true" to populate entity3.
> This
> corresponds to having a request in the form
> entity.entity2List[n].entity3.id (n=0,1,2...depending on how many
> items
> are selected).
> 
> Question: how should the name parameter of the s:select be like to
> achieve this?
> 
> Me and Lukasz  have tried the following without success (meaning
> entity.entity2List has is empty):
> <s:select key="centre.choose" multiple="true"
> name="%{entity.entity2List[].entity3.id}" list="#someList"
> listKey="id"
> listValue="code"/>
> <s:select key="centre.choose" multiple="true"
> name="entity.entity2List[].entity3.id" list="#someList" listKey="id"
> listValue="code"/>