Spring 3.0 MVC binds a nested object

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.

+7
java spring-form spring-mvc jsp spring-3
source share
1 answer

Can you try assigning the RegistrationBean to the appropriate receiver / setter.

 public class RegistrationBean { @Valid SecurityQuestion securityQuestion; String name; public SecurityQuestion getSecurityQuestion(){ return securityQuestion; } public void setSecurityQuestion(SecurityQuestion q){ this.securityQuestion = q; } public SecurityQuestionType[] getSecurityQuestionOptions() { return SecurityQuestionType.values(); } } 
+7
source share

All Articles