Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC - Setting bean from form data. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Code:
    <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>
    
    <bean id="myURL" class="java.net.URL">
            <constructor-arg index="0"><value>https://10.10.60.10/app</value> </constructor-arg>
        </bean>

    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
    http://static.springsource.org/docs/...tep/part1.html. Hope this will help

    Code:
      <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"/>
      </bean>
    When you start to implement your application and encounter specific errors, then post in here. )

    Comment


    • #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.

      Thanks.

      Comment


      • #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.
        Code:
        <bean id="si" class="com.sty.wor.ServiceInstance"/>
        <br/>
        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

        Comment

        Working...
        X