Reputation: 11
I have checkboxes tag in my web application with spring mvc. Checkboxes are created from a map in controller like this:
Map demOrgs = createMap(); model.addAttribute("demOrgs", demOrgs); // example : (1, my-description)
1 --> will be value of checkbox my-description --> will be label of checkbox
In my jsp :
<form:form commandName="myBean" method="POST" >
<form:checkboxes items="${demOrgs}" path="demOrg" element='div class="checkboxes"' />
</form:form>
My bean has only one field :
String demOrg;
When I send the form demOrg attribute has the value of checkboxes clicked, for example: (1,5,8)
I store myBean in session, when I go to the next step in my application. But when I return, I want the checkboxes were checked, still checked and isn't that way.
When the bind value of checkbox is a boolean value, allways work but I'm binding a custom value :
<input id="demOrg1" type="checkbox" value="2" name="demOrg">
<label for="demOrg1">My label description</label>
<input id="demOrg2" type="checkbox" value="3" name="demOrg">
<label for="demOrg2">My label description 2</label>
.....
Does anyone know how to do this?
thanks to all!!
Upvotes: 1
Views: 4705
Reputation: 3658
What does the signature of your controller method look like? Are you including myBean as a method signature argument, annotated with @ModelAttribute ?
Something like:
@RequestMapping(......)
public String myController (@ModelAttribute MyBeanType myBean, Model model) {
Map demOrgs = createMap();
model.addAttribute("demOrgs", demOrgs);
model.addAttribute(myBean);
}
Optionally you can annotate the method parameter with @Valid as well if you are using JSR-303 bean validation .
Upvotes: 1
Reputation: 1
Though "myBean" is stored in the session, isn't it reloaded again from the database when the controller is ran?
Upvotes: 0
Reputation: 1
I think the trick is to make sure your demOrg property is actually a collection. Check out the checkbox reference here. In particular, the text that says:
Typically the bound property is a collection so it can hold multiple values selected by the user.
Upvotes: 0