Announcement Announcement Module
Collapse
No announcement yet.
form tag's path attribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form tag's path attribute

    Hello,

    Suppose in a bean we have a property which is an object of some other class.For e.g
    Code:
    class Person{
       
         Address pimaryAddress;
         Address secondaryAddress;
         ...getters and setters..
    
    }
    
    Now Address class is also a bean as follows:
    
    class Address{
        
           String street;
           String state;
           String country;
    ....getters and setters
    
    
    
    }

    Now can we use in the path attribute of the Spring's form tag the following thing.

    <form:input path="primaryAddress.street" maxlength="100" />
    <form:input path="primaryAddress.city" maxlength="100" />.....

  • #2
    Yes you can. It is also explained in the reference documentation.

    Comment


    • #3
      Thanks mdeinum.

      I have tried it and it is valid.But i am facing a problem while doing so.

      The thing is: i have a jsp form in which a URL mapped to my form controller.
      In the jsp form i am using the form tags for providing input fields.


      employeeRegistration.jsp

      Code:
      .....
      <form:form commandName="employee" method="post">
      
      
      					<table cellpadding="5%" cellspacing="5%">
      						<tr>
      							<td> 
      								<form:input path="primaryAddress.street" maxlength="100" size="15" />
      							</td>
      							
      							
      							<td> 
      								<form:input path="primaryAddress.city" maxlength="100" size="15"/> </td>
      
      .....
      
      </tr>
      </table>
      
      </form:form>
      
      ..
      Now in my application-servlet.xml file i have provide the following:

      Code:
      <url-mapping bean configuration ....>
      
      <bean id="employeeRegistrationForm" class="employee.EmployeeRegistrationFormController">
      		<property name="sessionForm">
      			<value>true</value>
      		</property>	
      		
      		<property name="commandName"> 
      			<value>employee</value>
      		</property>
      		<property name="commandClass">
      			<value>employee.model.Employee</value>
      		</property>
      		
      		<property name="formView">
      			<value>employeeRegistration</value>
      		</property>
      				
      	</bean>
      
      <view-resolver configuration..>
      Now what is happening is when i click the link from the index page to get to employeeRegistration.jsp i am getting error:

      org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'primaryAddress' of bean class [eis.modules.employee.business.Employee]: Value of nested property 'primaryAddress' is null

      The command class is:
      Code:
      class Employee{
            .....
           Address pimaryAddress;
           Address secondaryAddress;
           ...getters and setters..
      
      }
      The Address class being referred in above class is also a bean as follows:

      Code:
      class Address{
          
             String street;
             String state;
             String country;
      ....getters and setters
      
      }
      What i think is that the Address object is not available hence it is giving error for null.

      So i tried provided in my application-servlet.xml the following:

      Code:
      <bean id="addressBean" class="employee.model.Address" />
      <bean id="employeeBean" class = "employee.model.Employee">
      		<property name="primaryAddress">
      			<ref bean="addressBean"/>
      		</property>
      		<property name="secondaryAddress">
      			<ref bean="addressBean"/>
      		</property>
      				
      </bean>
      and refer this bean in the section of property name="command" in the <bean> configuration for formController.

      But it is not working.

      Please help me getting out of this problem.


      Thanks.

      Comment


      • #4
        Your commandName is "employee". So try:

        employee.primaryAddress.street

        Comment


        • #5
          Hi Asha'man,
          in the <form> tag's attribute i have already specified the name as follows:

          <form:form commandName="employee" method="post">

          Will this not suffice for the thing you are asking to do??

          Comment


          • #6
            Yes, you're right, sorry. I'm not yet using the <form:form ....> tag.

            If I'll have ten minutes at lunch time, I will try to adapt that for one of my forms and see if it works for me and let you know.

            Comment


            • #7
              Thanks a lot Asha'man.

              Would appreciate help from other experts also.

              Thanks

              Comment


              • #8
                What you have is correct.

                The problem lies in your initialisation of the Employee object. If you want to use nestedpaths make sure all the referenced nested objects are initialized.

                Code:
                <bean id="addressBean" class="employee.model.Address" />
                <bean id="employeeBean" class = "employee.model.Employee">
                	<property name="primaryAddress">
                		<ref bean="addressBean"/>
                	</property>
                	<property name="secondaryAddress">
                		<ref bean="addressBean"/>
                	</property>				
                </bean>
                The above is not going to work (at least not as you expect) if you configure it this way your employee and addresses are going to be singeltons so every form is using the same instances.

                I would just construct a new Address instance in the constructor of the Employee object. That is the place to make sure that your object is correctly instantiated.

                Comment


                • #9
                  Hello Marten,

                  Can you please elaborate on below thing:

                  The above is not going to work (at least not as you expect) if you configure it this way your employee and addresses are going to be singeltons so every form is using the same instances.

                  Thanks

                  Comment


                  • #10
                    The beans as you defined in your context are Singletons by default. So one instance of it in the ApplicationContext. So if you are referencing to this instance in session 1 of user 1 and in session 2 of user 2 you are going to have problems.

                    If you really need something like this make sure that they are prototype beans. Everytime an instance is request new instances are made. However in your case I would modify the Employee object to make sure it is initialized correctly.

                    Comment

                    Working...
                    X