Why doesn't spring bind values ββto my nested object?
The SecurityQuestion object in the RegistrationBean is asked with the question and answer as null, null, respectively, despite the fact that it is defined in the form using the bean in the view.
Beans:
public class SecurityQuestion { SecurityQuestionType type; String answer; } public class RegistrationBean { @Valid SecurityQuestion securityQuestion; String name; public SecurityQuestionType[] getSecurityQuestionOptions() { return SecurityQuestionType.values(); } }
View:
<form:form modelAttribute="registrationBean" method="POST"> <form:select id="securityQuestion" path="securityQuestion.question"> <c:forEach var="securityQuestionOption" items="${securityQuestionOptions}"> <form:option value="${securityQuestionOption}">${securityQuestionOption</form:option> </c:forEach> </form:select> <form:input id="securityAnswer" path="securityQuestion.answer" /> <form:input id="name" path="name" /> </form:form>
Controller:
@RequestMapping(value = URL_PATTERN, method = RequestMethod.POST) public ModelAndView submit(@Valid final RegistrationBean registrationBean) { // registrationBean.getSecurityQuestion().getQuestion() == null // registrationBean.getSecurityQuestion().getAnswer() == null }
Decision
All beans must have getters / setters for all fields. spring uses the default constructor, and then uses setters to mutate the object from the view.
java spring-form spring-mvc jsp spring-3
dom farr
source share