I have a pretty simple controller that 1) loads an object to be used by a form (view) and 2) accepts changes to that object submitted via that form. The controller (“PersonController”) looks like this:
@RequestMapping(value="/accountsettings/{personid}")
public String loadAccountSettings(@PathVariable("personid") int personid,
ModelMap model){
Person person = personService.find(personid);
model.addAttribute("person",person);
return "account-settings-form";
}
@RequestMapping(value="/saveaccountsettings")
public String saveSettings(@ModelAttribute("person") Person person){
personService.update(person);
return "account-settings-form";
}
The “Person” pojo looks like this:
public class Person {
private id int;
private String name;
private String username;
private String phoneNumber;
//getters and setters follow
}
And my form (account-settings-form.jsp) has the following:
<form:form commandName="person" action="/saveaccountsettings">
<form:input path="name" /><br />
<form:input path="username" /><br />
<form:input path="phoneNumber" /><br />
<input type="submit" />
</form:form>
The problem I’m having is that, when the form loads the data (loadAccountSettings), all fields are populated as expected (name, username and phone number). When I go to process the update, however, the phone number field is seen as null, even though data is in the form and (upon inspection using Firebug), I see the value being passed back as a parameter.
Does anyone know why the controller would be missing this field’s (person.phoneNumber) value?
Thanks in advance.
-e
I figured out the problem, a part of the Controller that I left out in the description above and forgot that I added several months ago – an InitBinder method. The phone number field was added recently, but wasn’t added to the @InitBinder’s array of allowedFields: