Results 1 to 7 of 7

Thread: Binding complex object with forms

  1. #1
    Join Date
    Feb 2006
    Posts
    24

    Default Binding complex object with forms

    Hi Team,

    I have the following form which I need to submit.
    Code:
    <form method="POST" action="registerUser.html">
    <table border="0" cellspacing="5" cellpadding="0">
    	<tr>
    		<td>First Name :<input type="text" name="firstname"></td>
    	</tr>
    	<tr>
    		<td>Last Name :<input type="text" name="lastname"></td>
    	</tr>
    	<tr>
    		<td>Street :<input type="text" name="address.street"></td>
    	</tr>
    	<tr>
    		<td>State :<input type="text" name="address.state"></td>
    	</tr>
    </table>
    </form>

    The data is validated and then then passed on the controller.

    Configuration file
    Code:
    	<bean id="addressValidator" class="com.skyline.foundation.web.AddressValidator"/>
    
    	<bean id="userRegistrationValidator" class="com.skyline.foundation.web.UserRegistrationValidator">
    		<constructor-arg><ref bean="addressValidator"/></constructor-arg>
    	</bean>
    
    	<bean id="registerUserController"
    		class="com.skyline.foundation.web.RegisterUserController">			
    	    <property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>user</value></property>
            <property name="commandClass"><value>com.skyline.foundation.model.User</value></property>
            <property name="validator"><ref bean="userRegistrationValidator"/></property>
            <property name="formView"><value>registerUser</value></property>
            <property name="successView"><value>registerSuccess.htm</value></property>
            <property name="userService"><ref bean="userService" /></property>
    	</bean>

    The user object is as defined below and has the setter and getter for the attributed

    Code:
    public class User implements Serializable, Comparable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private Long id = null;
    	private int version;
    	private String firstname;
    	private String lastname;
    	private Address address;
    The Address class is as below
    Code:
    	public class Address implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private String street;
    	private String country;

    When ever I am submitting my form I am getting the following exception
    Code:
    org.springframework.beans.NullValueInNestedPathException: Invalid property 'address' of bean class [com.skyline.foundation.model.User]: Value of nested property 'address' is null
    	org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:419)
    	org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:394)
    	org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:600)
    	org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
    	org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
    	org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:517)
    	org.springframework.validation.DataBinder.doBind(DataBinder.java:419)
    	org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:147)
    	org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:108)
    	org.springframework.web.servlet.mvc.BaseCommandController.bindAndValidate(BaseCommandController.java:369)
    	org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:248)
    	org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:820)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    Please help me how to bind the form with a complex command object.

    Regards
    Om

  2. #2
    Join Date
    Mar 2006
    Posts
    312

    Default

    Your User constructor needs to instantiate an Address.

  3. #3
    Join Date
    Feb 2006
    Posts
    24

    Default

    I have a constructor as well which is as below

    Code:
    public class User implements Serializable, Comparable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private Long id = null;
    	private int version;
    	private String firstname;
    	private String lastname;
    	private Address address;
    
    	/**
    	 * No-arg constructor for JavaBean tools.
    	 */
    	User() {
    	}
    
    	/**
    	 * Full constructor.
    	 */
    	public User(String firstname, String lastname, Address address) {
    		this.firstname = firstname;
    		this.lastname = lastname;
    		this.address = address;
    	}

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Quote Originally Posted by jglynn
    Your User constructor needs to instantiate an Address.
    your constructor and Address declaration

    Code:
    private Address address;
    User() {}
    The rest should be easy to figure out by now....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2006
    Posts
    24

    Default

    Code:
    	/**
    	 * Full constructor.
    	 */
    	public User(String firstname, String lastname, Address address) {
    		this.firstname = firstname;
    		this.lastname = lastname;
    		this.address = address;
    	}
    I already have a constructor with above defination

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Well yeah you have BUT that one isn't used by yuor FormController. A new user object wil be created with a new User() (Spring uses reflection for that). So NO Address object is instantiated...

    I suggest I readup on how the framework works, read chapter 13 of the reference guide and take a look at the javadoc.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Thumbs up

    Instantiate Address in User bean.
    EXample: private Address address = new Address();
    Enjoy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •