Spring 3.0 MVC binding nested object
Asked Answered
B

1

7

Why is spring not binding the values on my nested object?

The SecurityQuestion object on the RegistrationBean is set with question and answer as null, null, respectively, despite setting then in form using bean notation 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
}

Solution

All beans have to have getters/setters for all fields. Spring uses the default constructor and then uses the setters to mutate the object from the view.

Burney answered 8/12, 2010 at 19:17 Comment(1)
question attribute is missing in SecurityQuestion (I suppose it is because you have forgotten to write it in the question). Do you have getters and setters for the attributes?Extine
R
7

Can you try giving the RegistrationBean an appropriate getter/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();
  }
}
Rakes answered 8/12, 2010 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.