Context
I have a simple association between two objects - Category and Email (NtoM). I am trying to create a web interface for viewing and managing them. To browse a category and add emails to this category, I use a controller wrapped in @RequestMapping with a category identifier (UUID), so all controller actions always occur in the context of the category specified by the path.
I use @ModelAttribute to preload the context category for the entire control area.
Problem
This approach is well established for displaying and displaying forms. However, this fails when submitting the form - after a bit of debugging, I found that the form data overrides the parameter of my @ModelAttribute category.
In my code in the save() method, the Category is actually not a model attribute loaded with the addCategory() method, but it is populated with form data (the t28> model is also populated, and this is correct).
I am looking for a solution that will allow me to bind form data only to a specific @ModelAttribute .
I read in the Spring MVC documentation that the order of the arguments matters, but I ordered them according to the examples, and yet it doesn't work as expected.
The code
Here is my controller:
@Controller @RequestMapping("/emails/{categoryId}") public class EmailsController { @ModelAttribute("category") public Category addCategory(@PathVariable UUID categoryId) { return this.categoryService.getCategory(categoryId); } @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Set.class, "categories", new CategoriesSetEditor(this.categoryService)); } @RequestMapping(value = "/create", method = RequestMethod.GET) public String createForm(@ModelAttribute Category category, Model model) {
Just in case, here is also the form code:
<form:form action="${pageContext.request.contextPath}/emails/${category.id}/save" method="post" modelAttribute="email"> <form:hidden path="id"/> <fieldset> <label for="emailName"><spring:message code="email.form.label.Name" text="E-mail address"/>:</label> <form:input path="name" id="emailName" required="required"/> <form:errors path="name" cssClass="error"/> <label for="emailRealName"><spring:message code="email.form.label.RealName" text="Recipient display name"/>:</label> <form:input path="realName" id="emailRealName"/> <form:errors path="realName" cssClass="error"/> <label for="emailIsActive"><spring:message code="email.form.label.IsActive" text="Activation status"/>:</label> <form:checkbox path="active" id="emailIsActive"/> <form:errors path="active" cssClass="error"/> <form:checkboxes path="categories" element="div" items="${categories}" itemValue="id" itemLabel="name"/> <form:errors path="categories" cssClass="error"/> <button type="submit"><spring:message code="_common.form.Submit" text="Save"/></button> </fieldset> </form:form>
Note: I do not want several @ModelAttribute to come from POST, just want to distinguish somehow the form of the model from the previously generated attributes.
java spring spring-annotations spring-mvc annotations
RafaΕ Wrzeszcz
source share