You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Guillaume Bilodeau <gb...@yahoo.com> on 2007/09/23 19:48:40 UTC
Mapping multiple enums to a nested object
Hi all,
I'm quite new to Struts2 and so far I love what I see. Currently I'm having
a problem trying to map a checkboxlist based on a set of enums to a model
object nested in my action. Here's what I have now:
JSP:
Action:
public class CreateSimpleFoodAction {
private FoodService foodService;
private SimpleFoodCreationRequest foodCreationRequest = new
SimpleFoodCreationRequest();
public CreateSimpleFoodAction() {
}
public String prepareNewSimpleFood() {
return Action.SUCCESS;
}
public String createSimpleFood() {
String result;
try {
foodService.createSimpleFood(foodCreationRequest);
result = Action.SUCCESS;
}
catch (FoodException e) {
result = Action.INPUT;
}
return result;
}
public SimpleFoodCreationRequest getModel() {
return foodCreationRequest;
}
public List getAvailableGroups() {
return Arrays.asList(Group.values());
}
public List getAvailableNutrients() {
return Arrays.asList(Nutrient.values());
}
public void setFoodService(FoodService foodService) {
this.foodService = foodService;
}
}
Model:
public class SimpleFoodCreationRequest implements SimpleFoodPropertyProvider
{
private String name;
private Group group;
private Set nutrients = new HashSet();
public SimpleFoodCreationRequest() {
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSelectedNutrients(Nutrient[] selectedNutrients) {
nutrients.clear();
for (Nutrient n : selectedNutrients) {
nutrients.add(n);
}
}
public Nutrient[] getSelectedNutrients() {
return nutrients.toArray(new Nutrient[0]);
}
public Set getNutrients() {
return Collections.unmodifiableSet(nutrients);
}
}
CreateSimpleFoodAction-conversion.properties:
model.group=com.opensymphony.xwork2.util.EnumTypeConverter
model.selectedNutrients=com.opensymphony.xwork2.util.EnumTypeConverter
Element_model.selectedNutrients=com.gb1.healthcheck.domain.nutrition.Nutrient
When I submit this form, the selected checkbox list items are not mapped to
my SimpleFoodCreationRequest object. The console shows the standard message
"error setting 'model.selectedNutrients' with..."
Any ideas?
Thanks!
GB
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12848740
Sent from the Struts - User mailing list archive at Nabble.com.
Re: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Ok that clears things up. I'll sit down and try to write this down now.
Thanks for all the help guys!
GB
cilquirm wrote:
>
> Doesn't seem that way.
> One converts a single string value to an enum, and the other converts a
> String[] to a List<Enum>
>
> I just checked it over and it's not directly useable because it's a type
> converter for a specific enum ( OperationsEnum ), but the principles
> apply.
>
> The long and the short of is that you'll have to write your own type
> converter, but checking out the example from showcase is a fairly good
> starting point that should get you at least halfway.
>
> hth,
> -a
>
>
> Guillaume Bilodeau wrote:
>>
>> So you're saying that the showcase EnumTypeConverter isn't the same as
>> the one provided in XWork2?
>>
>>
>> cilquirm wrote:
>>>
>>> Indeed, the problem is that EnumTypeConverter, as provided by Struts2
>>> only works for single values.
>>>
>>> Can you make selectedNutrients a List?
>>>
>>> If so, you might be able to use the EnumTypeConverter from the showcase
>>> app, or roll your own based off it, which isn't that hard at all.
>>>
>>> org.apache.struts2.showcase.conversion.EnumTypeConverter
>>>
>>> hth,
>>> -a
>>>
>>>
>>>
>>> Guillaume Bilodeau wrote:
>>>>
>>>> Yes I tried that, with no success unfortunately.
>>>>
>>>> Somehow we need to specify that:
>>>> 1- selectedNutrients[] will need to be mapped to an array of Nutrients
>>>> 2- each element of selectedNutrients[] must be mapped using
>>>> EnumTypeConverter
>>>>
>>>>
>>>> cilquirm wrote:
>>>>>
>>>>> Are enums type-converted automatically now?
>>>>>
>>>>> I recall early on I had to specify
>>>>> http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
>>>>>
>>>>> Have you tried specifying that?
>>>>>
>>>>> with
>>>>>
>>>>> Element_selectedNutrients =
>>>>> com.opensymphony.xwork2.util.EnumTypeConverter
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12886927
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: Mapping multiple enums to a nested object
Posted by cilquirm <aa...@gmail.com>.
Doesn't seem that way.
One converts a single string value to an enum, and the other converts a
String[] to a List<Enum>
I just checked it over and it's not directly useable because it's a type
converter for a specific enum ( OperationsEnum ), but the principles apply.
The long and the short of is that you'll have to write your own type
converter, but checking out the example from showcase is a fairly good
starting point that should get you at least halfway.
hth,
-a
Guillaume Bilodeau wrote:
>
> So you're saying that the showcase EnumTypeConverter isn't the same as the
> one provided in XWork2?
>
>
> cilquirm wrote:
>>
>> Indeed, the problem is that EnumTypeConverter, as provided by Struts2
>> only works for single values.
>>
>> Can you make selectedNutrients a List?
>>
>> If so, you might be able to use the EnumTypeConverter from the showcase
>> app, or roll your own based off it, which isn't that hard at all.
>>
>> org.apache.struts2.showcase.conversion.EnumTypeConverter
>>
>> hth,
>> -a
>>
>>
>>
>> Guillaume Bilodeau wrote:
>>>
>>> Yes I tried that, with no success unfortunately.
>>>
>>> Somehow we need to specify that:
>>> 1- selectedNutrients[] will need to be mapped to an array of Nutrients
>>> 2- each element of selectedNutrients[] must be mapped using
>>> EnumTypeConverter
>>>
>>>
>>> cilquirm wrote:
>>>>
>>>> Are enums type-converted automatically now?
>>>>
>>>> I recall early on I had to specify
>>>> http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
>>>>
>>>> Have you tried specifying that?
>>>>
>>>> with
>>>>
>>>> Element_selectedNutrients =
>>>> com.opensymphony.xwork2.util.EnumTypeConverter
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12885920
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: Mapping multiple enums to a nested object
Posted by Dave Newton <ne...@yahoo.com>.
--- Guillaume Bilodeau <gb...@yahoo.com> wrote:
> So you're saying that the showcase EnumTypeConverter
> isn't the same as the one provided in XWork2?
The showcase converter specifically handles
conversions to/from Lists; the XWork converter
converts to/from Strings. It is poorly named, perhaps,
and could arguably be put somewhere more useful.
d.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
So you're saying that the showcase EnumTypeConverter isn't the same as the
one provided in XWork2?
cilquirm wrote:
>
> Indeed, the problem is that EnumTypeConverter, as provided by Struts2
> only works for single values.
>
> Can you make selectedNutrients a List?
>
> If so, you might be able to use the EnumTypeConverter from the showcase
> app, or roll your own based off it, which isn't that hard at all.
>
> org.apache.struts2.showcase.conversion.EnumTypeConverter
>
> hth,
> -a
>
>
>
> Guillaume Bilodeau wrote:
>>
>> Yes I tried that, with no success unfortunately.
>>
>> Somehow we need to specify that:
>> 1- selectedNutrients[] will need to be mapped to an array of Nutrients
>> 2- each element of selectedNutrients[] must be mapped using
>> EnumTypeConverter
>>
>>
>> cilquirm wrote:
>>>
>>> Are enums type-converted automatically now?
>>>
>>> I recall early on I had to specify
>>> http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
>>>
>>> Have you tried specifying that?
>>>
>>> with
>>>
>>> Element_selectedNutrients =
>>> com.opensymphony.xwork2.util.EnumTypeConverter
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12885660
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: Mapping multiple enums to a nested object
Posted by cilquirm <aa...@gmail.com>.
Indeed, the problem is that EnumTypeConverter, as provided by Struts2 only
works for single values.
Can you make selectedNutrients a List?
If so, you might be able to use the EnumTypeConverter from the showcase app,
or roll your own based off it, which isn't that hard at all.
org.apache.struts2.showcase.conversion.EnumTypeConverter
hth,
-a
Guillaume Bilodeau wrote:
>
> Yes I tried that, with no success unfortunately.
>
> Somehow we need to specify that:
> 1- selectedNutrients[] will need to be mapped to an array of Nutrients
> 2- each element of selectedNutrients[] must be mapped using
> EnumTypeConverter
>
>
> cilquirm wrote:
>>
>> Are enums type-converted automatically now?
>>
>> I recall early on I had to specify
>> http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
>>
>> Have you tried specifying that?
>>
>> with
>>
>> Element_selectedNutrients =
>> com.opensymphony.xwork2.util.EnumTypeConverter
>>
>
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12885311
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: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Yes I tried that, with no success unfortunately.
Somehow we need to specify that:
1- selectedNutrients[] will need to be mapped to an array of Nutrients
2- each element of selectedNutrients[] must be mapped using
EnumTypeConverter
cilquirm wrote:
>
> Are enums type-converted automatically now?
>
> I recall early on I had to specify
> http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
>
> Have you tried specifying that?
>
> with
>
> Element_selectedNutrients = com.opensymphony.xwork2.util.EnumTypeConverter
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12885091
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: Mapping multiple enums to a nested object
Posted by cilquirm <aa...@gmail.com>.
Are enums type-converted automatically now?
I recall early on I had to specify
http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/util/EnumTypeConverter.html
Have you tried specifying that?
with
Element_selectedNutrients = com.opensymphony.xwork2.util.EnumTypeConverter
Guillaume Bilodeau wrote:
>
> Looks like the JSP code doesn't show. Here it is:
>
> <pre>
> [s:form action="createSimpleFood.go" method="post"
> namespace="/nutrition"]
> [s:textfield key="food.name" name="model.name" /][br/]
> [s:select key="food.group" name="model.group"
> list="availableGroups" /]
> [s:checkboxlist key="food.nutrients"
> name="model.selectedNutrients"
> list="availableNutrients" /]
> [s:submit key="nutrition.foods.simpleFoods.create.submit" /]
> [/s:form]
> </pre>
>
>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12884986
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: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Looks like the JSP code doesn't show. Here it is:
<pre>
[s:form action="createSimpleFood.go" method="post"
namespace="/nutrition"]
[s:textfield key="food.name" name="model.name" /][br/]
[s:select key="food.group" name="model.group"
list="availableGroups" /]
[s:checkboxlist key="food.nutrients"
name="model.selectedNutrients"
list="availableNutrients" /]
[s:submit key="nutrition.foods.simpleFoods.create.submit" /]
[/s:form]
</pre>
--
View this message in context: http://www.nabble.com/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12852427
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: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
I've done some further debugging and here's what I now know:
1. I was able to bind TypeConverters to the model.group and
model.selectedNutrients properties by creating a
SimpleFoodCreationRequest-conversion.properties with the following lines:
group=com.opensymphony.xwork2.util.EnumTypeConverter
selectedNutrients=com.opensymphony.xwork2.util.EnumTypeConverter
Element_selectedNutrients=com.gb1.healthcheck.domain.nutrition.Nutrient
So converters can be defined at the nested object's level.
2. The group property is correctly mapped, the conversion being done by the
EnumTypeConverter.
3. The selectedNutrients property is still not being mapped correctly. The
EnumTypeConverter is invoked to map the whole parameter array ([VITAMIN_A,
VITAMIN_B]) to an instance of Nutrient[]. To do this, it arbitrarly chooses
the first parameter (parameters[0] => VITAMIN_A) and tries to map it by
calling Enum.valueOf(Nutrient[].class, "VITAMIN_A"). This obviously fails.
Why doesn't Struts2 / XWork recognize that there are multiple parameters to
be mapped to multiple enums and call the EnumTypeConverter for each of them,
sending the resulting Nutrient[] to the request object? Do I need to
specify something else?
Thanks,
GB
Guillaume Bilodeau wrote:
>
> Hey no problem, I'm kind of sluggish also :) It's true that I could use a
> different method name to expose the nested object, it does bring some
> confusion.
>
> Somehow the group converter seems to work fine, so I don't understand why
> the nutrients doesn't. I'm knee-deep in OGNL code to see exactly what's
> going on now.
>
>
> newton.dave wrote:
>>
>> Whoops!
>>
>> Guillaume Bilodeau wrote:
>>> 1- I'm using "food.name" as the text field key - not
>>> as its name - so a localized label will be shown
>> next
>>> to it.
>>
>> You're right; I didn't even see that.
>>
>>> 2- I thought the action itself was pushed on the
>>> value stack and that one could reference and bind
>>> parameters to nested objects in the JSP.
>>
>> That is also correct; using the interface method name
>> threw me off (I probably wouldn't do that, although
>> there's no reason you can't).
>>
>> *sigh* Gonna be one of those Mondays.
>>
>> Does the "group" converter work properly?
>>
>> d.
>>
>>
>> ---------------------------------------------------------------------
>> 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/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12883987
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: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Hey no problem, I'm kind of sluggish also :) It's true that I could use a
different method name to expose the nested object, it does bring some
confusion.
Somehow the group converter seems to work fine, so I don't understand why
the nutrients doesn't. I'm knee-deep in OGNL code to see exactly what's
going on now.
newton.dave wrote:
>
> Whoops!
>
> Guillaume Bilodeau wrote:
>> 1- I'm using "food.name" as the text field key - not
>> as its name - so a localized label will be shown
> next
>> to it.
>
> You're right; I didn't even see that.
>
>> 2- I thought the action itself was pushed on the
>> value stack and that one could reference and bind
>> parameters to nested objects in the JSP.
>
> That is also correct; using the interface method name
> threw me off (I probably wouldn't do that, although
> there's no reason you can't).
>
> *sigh* Gonna be one of those Mondays.
>
> Does the "group" converter work properly?
>
> d.
>
>
> ---------------------------------------------------------------------
> 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/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12860466
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: Mapping multiple enums to a nested object
Posted by Dave Newton <ne...@yahoo.com>.
Whoops!
Guillaume Bilodeau wrote:
> 1- I'm using "food.name" as the text field key - not
> as its name - so a localized label will be shown
next
> to it.
You're right; I didn't even see that.
> 2- I thought the action itself was pushed on the
> value stack and that one could reference and bind
> parameters to nested objects in the JSP.
That is also correct; using the interface method name
threw me off (I probably wouldn't do that, although
there's no reason you can't).
*sigh* Gonna be one of those Mondays.
Does the "group" converter work properly?
d.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Ok, as I said I'm new to Struts2 so maybe I made some wrong assumptions.
Please correct me if I'm wrong:
1- I'm using "food.name" as the text field key - not as its name - so a
localized label will be shown next to it.
2- I thought the action itself was pushed on the value stack and that one
could reference and bind parameters to nested objects in the JSP.
Am I mistaken?
Thanks for your help,
GB
newton.dave wrote:
>
> --- Guillaume Bilodeau <gb...@yahoo.com> wrote:
>> Yes it was deliberate. I'm not sure I see the
>> advantages of flattening my action *and* my request
>> object, for now I'd rather keep this nested as it
>> currently is. Of course I'll change my mind if
>> given the right counter-arguments :)
>
> The most compelling counter-argument is that
> getModel() doesn't expose anything to the JSP unless
> you implement ModelDriven.
>
> You reference "food.name" etc. on your form but don't
> expose anything named "food", which is what Adam was
> referring to.
>
> Even if you *did* implement ModelDriven you would
> still not be exposing anything named "food" to the
> JSP: the result of the getModel() call is pushed on to
> the stack, properties are accessed by name (without
> the bean name), like <s:property value="name"/>.
>
> d.
>
>
> ---------------------------------------------------------------------
> 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/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12860163
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: Mapping multiple enums to a nested object
Posted by Dave Newton <ne...@yahoo.com>.
--- Guillaume Bilodeau <gb...@yahoo.com> wrote:
> Yes it was deliberate. I'm not sure I see the
> advantages of flattening my action *and* my request
> object, for now I'd rather keep this nested as it
> currently is. Of course I'll change my mind if
> given the right counter-arguments :)
The most compelling counter-argument is that
getModel() doesn't expose anything to the JSP unless
you implement ModelDriven.
You reference "food.name" etc. on your form but don't
expose anything named "food", which is what Adam was
referring to.
Even if you *did* implement ModelDriven you would
still not be exposing anything named "food" to the
JSP: the result of the getModel() call is pushed on to
the stack, properties are accessed by name (without
the bean name), like <s:property value="name"/>.
d.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Yes it was deliberate. I'm not sure I see the advantages of flattening my
action *and* my request object, for now I'd rather keep this nested as it
currently is. Of course I'll change my mind if given the right
counter-arguments :)
newton.dave wrote:
>
> --- Guillaume Bilodeau <gb...@yahoo.com> wrote:
>> The action has a SimpleFoodCreationRequest
> getModel()
>> method which should expose the request object to
>> the JSP.
>
> The Action code you posted does not implement
> ModelDriven; was that a deliberate omission?
>
>> Action:
>> public class CreateSimpleFoodAction {
>> private FoodService foodService;
>> // etc...
>
> d.
>
>
> ---------------------------------------------------------------------
> 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/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12859800
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: Mapping multiple enums to a nested object
Posted by Dave Newton <ne...@yahoo.com>.
--- Guillaume Bilodeau <gb...@yahoo.com> wrote:
> The action has a SimpleFoodCreationRequest
getModel()
> method which should expose the request object to
> the JSP.
The Action code you posted does not implement
ModelDriven; was that a deliberate omission?
> Action:
> public class CreateSimpleFoodAction {
> private FoodService foodService;
> // etc...
d.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Mapping multiple enums to a nested object
Posted by Guillaume Bilodeau <gb...@yahoo.com>.
Hi Adam,
The action has a
SimpleFoodCreationRequest getModel()
method which should expose the request object to the JSP. However you are
right that it doesn't have any setter, but is it really necessary since all
bindings are done on the nested request object?
Thanks,
GB
Adam Hardy-3 wrote:
>
> Guillaume,
>
> I don't see a Food getter and setter on your action.
>
> Guillaume Bilodeau on 23/09/07 18:48, wrote:
>> Hi all,
>>
>> I'm quite new to Struts2 and so far I love what I see. Currently I'm
>> having
>> a problem trying to map a checkboxlist based on a set of enums to a model
>> object nested in my action. Here's what I have now:
>>
>> JSP:
> [s:form action="createSimpleFood.go" method="post"
> namespace="/nutrition"]
> [s:textfield key="food.name" name="model.name" /][br/]
> [s:select key="food.group" name="model.group"
> list="availableGroups" /]
> [s:checkboxlist key="food.nutrients"
> name="model.selectedNutrients"
> list="availableNutrients" /]
> [s:submit key="nutrition.foods.simpleFoods.create.submit" /]
> [/s:form]
>
>>
>> Action:
>>
>>
>> public class CreateSimpleFoodAction {
>> private FoodService foodService;
>> private SimpleFoodCreationRequest foodCreationRequest = new
>> SimpleFoodCreationRequest();
>>
>> public CreateSimpleFoodAction() {
>> }
>>
>> public String prepareNewSimpleFood() {
>> return Action.SUCCESS;
>> }
>>
>> public String createSimpleFood() {
>> String result;
>>
>> try {
>> foodService.createSimpleFood(foodCreationRequest);
>> result = Action.SUCCESS;
>> }
>> catch (FoodException e) {
>> result = Action.INPUT;
>> }
>>
>> return result;
>> }
>>
>> public SimpleFoodCreationRequest getModel() {
>> return foodCreationRequest;
>> }
>>
>> public List getAvailableGroups() {
>> return Arrays.asList(Group.values());
>> }
>>
>> public List getAvailableNutrients() {
>> return Arrays.asList(Nutrient.values());
>> }
>>
>> public void setFoodService(FoodService foodService) {
>> this.foodService = foodService;
>> }
>> }
>>
>>
>> Model:
>>
>>
>> public class SimpleFoodCreationRequest implements
>> SimpleFoodPropertyProvider
>> {
>> private String name;
>> private Group group;
>> private Set nutrients = new HashSet();
>>
>> public SimpleFoodCreationRequest() {
>> }
>>
>> public Group getGroup() {
>> return group;
>> }
>>
>> public void setGroup(Group group) {
>> this.group = group;
>> }
>>
>> public String getName() {
>> return name;
>> }
>>
>> public void setName(String name) {
>> this.name = name;
>> }
>>
>> public void setSelectedNutrients(Nutrient[] selectedNutrients) {
>> nutrients.clear();
>> for (Nutrient n : selectedNutrients) {
>> nutrients.add(n);
>> }
>> }
>>
>> public Nutrient[] getSelectedNutrients() {
>> return nutrients.toArray(new Nutrient[0]);
>> }
>>
>> public Set getNutrients() {
>> return Collections.unmodifiableSet(nutrients);
>> }
>> }
>>
>>
>> CreateSimpleFoodAction-conversion.properties:
>>
>>
>> model.group=com.opensymphony.xwork2.util.EnumTypeConverter
>> model.selectedNutrients=com.opensymphony.xwork2.util.EnumTypeConverter
>> Element_model.selectedNutrients=com.gb1.healthcheck.domain.nutrition.Nutrient
>>
>>
>> When I submit this form, the selected checkbox list items are not mapped
>> to
>> my SimpleFoodCreationRequest object. The console shows the standard
>> message
>> "error setting 'model.selectedNutrients' with..."
>>
>> Any ideas?
>>
>> Thanks!
>> GB
>>
>
>
> ---------------------------------------------------------------------
> 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/Mapping-multiple-enums-to-a-nested-object-tf4505238.html#a12859530
Sent from the Struts - User mailing list archive at Nabble.com.
Re: Mapping multiple enums to a nested object
Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Guillaume,
I don't see a Food getter and setter on your action.
Guillaume Bilodeau on 23/09/07 18:48, wrote:
> Hi all,
>
> I'm quite new to Struts2 and so far I love what I see. Currently I'm having
> a problem trying to map a checkboxlist based on a set of enums to a model
> object nested in my action. Here's what I have now:
>
> JSP:
[s:form action="createSimpleFood.go" method="post"
namespace="/nutrition"]
[s:textfield key="food.name" name="model.name" /][br/]
[s:select key="food.group" name="model.group"
list="availableGroups" /]
[s:checkboxlist key="food.nutrients"
name="model.selectedNutrients"
list="availableNutrients" /]
[s:submit key="nutrition.foods.simpleFoods.create.submit" /]
[/s:form]
>
> Action:
>
>
> public class CreateSimpleFoodAction {
> private FoodService foodService;
> private SimpleFoodCreationRequest foodCreationRequest = new
> SimpleFoodCreationRequest();
>
> public CreateSimpleFoodAction() {
> }
>
> public String prepareNewSimpleFood() {
> return Action.SUCCESS;
> }
>
> public String createSimpleFood() {
> String result;
>
> try {
> foodService.createSimpleFood(foodCreationRequest);
> result = Action.SUCCESS;
> }
> catch (FoodException e) {
> result = Action.INPUT;
> }
>
> return result;
> }
>
> public SimpleFoodCreationRequest getModel() {
> return foodCreationRequest;
> }
>
> public List getAvailableGroups() {
> return Arrays.asList(Group.values());
> }
>
> public List getAvailableNutrients() {
> return Arrays.asList(Nutrient.values());
> }
>
> public void setFoodService(FoodService foodService) {
> this.foodService = foodService;
> }
> }
>
>
> Model:
>
>
> public class SimpleFoodCreationRequest implements SimpleFoodPropertyProvider
> {
> private String name;
> private Group group;
> private Set nutrients = new HashSet();
>
> public SimpleFoodCreationRequest() {
> }
>
> public Group getGroup() {
> return group;
> }
>
> public void setGroup(Group group) {
> this.group = group;
> }
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> this.name = name;
> }
>
> public void setSelectedNutrients(Nutrient[] selectedNutrients) {
> nutrients.clear();
> for (Nutrient n : selectedNutrients) {
> nutrients.add(n);
> }
> }
>
> public Nutrient[] getSelectedNutrients() {
> return nutrients.toArray(new Nutrient[0]);
> }
>
> public Set getNutrients() {
> return Collections.unmodifiableSet(nutrients);
> }
> }
>
>
> CreateSimpleFoodAction-conversion.properties:
>
>
> model.group=com.opensymphony.xwork2.util.EnumTypeConverter
> model.selectedNutrients=com.opensymphony.xwork2.util.EnumTypeConverter
> Element_model.selectedNutrients=com.gb1.healthcheck.domain.nutrition.Nutrient
>
>
> When I submit this form, the selected checkbox list items are not mapped to
> my SimpleFoodCreationRequest object. The console shows the standard message
> "error setting 'model.selectedNutrients' with..."
>
> Any ideas?
>
> Thanks!
> GB
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org