Announcement Announcement Module
No announcement yet.
Spring MVC - Setting bean from form data. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC - Setting bean from form data.

    I am creating a spring mvc application. I need to create a bean using the form data and that should be available via my application context in the rest of my application. Right now I have the following in my applicationContext.xml
    <bean id="si" class="com.sty.wor.ServiceInstance">
            <constructor-arg index="0"><ref bean="myURL"/></constructor-arg>
            <constructor-arg index="1"> <value>username</value> </constructor-arg>
            <constructor-arg index="2"><value>password</value> </constructor-arg>
            <constructor-arg index="3"><value>true</value> </constructor-arg>
    <bean id="myURL" class="">
            <constructor-arg index="0"><value></value> </constructor-arg>

    Please tell me how I can get the data for these bean creations from a form [I want to create these beans after the form data validation]. Thank you.

  • #2
    Your question is too vague. If you want to create sample form spring application, there have many choices for you. Are you planning to use annotation-base or xml configuration?
    Keep in mind, spring 3 recommends to use controller annotation. Here is a very useful link Hope this will help

      <bean name="/formName.htm" class="Controllerclass">  	 
      	<property name = "commandName" value="formName"/>
      	<property name="commandClass" value="formDTOclass"/>
      	<property name="formView" value="formName"/>
      	<property name="sessionForm" value="true"/>
      	<property name="customService" ref="si"/>
    When you start to implement your application and encounter specific errors, then post in here. )


    • #3
      Here is what I am doing now:

      I have a class called ServiceInstance which is part of a webservices jar provided by somebody. Now, I create this object with the information in applicationContext.xml and use this object in my business layer (data access layer). The data needed for the ServiceInstance object is hard-coded in applicationcontext.xml.

      Here is what I am trying to do:

      Create a form and read the data from the form fields to create the ServiceInstance object. Once I create this service instance object, I will access it from my business layer for various operations using applicationContext.

      Hope this clarifies.



      • #4
        1. Create controller for your form.
        2. Create custom DTO( domain transfer object)
        3. Map custom DTO to your controller.
        4. Remove wired constructor in your service since you wan to get those values from user's input. You put in there if you want to populate default values.
        <bean id="si" class="com.sty.wor.ServiceInstance"/>
        Second option:
        1. Since your webservice url probably doesn't change much, I recommend to move to properties file and use PropertyPlaceholderConfigurer to dynamic populate in your xml.
        2. Should user name and password for your webservice in database? If yes, I suggest you manually wire those properties in your services.

        If someone have better ideas, please leave feedback here. Thanks