0

I need help understanding some form handling. Suppose I have two pojo like below.

First:

public class Loginfo {
   private String username;
   private String password;

   // setters and getters ... 
} 

Second:

pubic class PersonalInfo {
    private String name;
    private String age;
    private Loginfo loginfo;

    // setters and getters... 
}

And lastly I have a form with the fields: username, password, name, age.

How do I handle this form? What should the controller code look like?

Neuron
  • 5,141
  • 5
  • 38
  • 59
torikraju
  • 7
  • 2
  • 6

2 Answers2

0

You would need a JSON converter in your application. You can use Jackson for this purpose. You will need the Jackson core,databind and annotation jars/dependencies for this. Make sure all three jars/dependencies are of same version.

Add below to dispatcher-servlet:

<beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>

The above code will directly convert objects to JSON and vice versa whenever an http request comes in to the controller.

The code in controller would look like:

@RequestMapping(value = "/serviceName", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody void service(@RequestBody PersonalInfo personalInfo){
}

Make sure the name of the var you post is personalInfo.

HARDI
  • 394
  • 5
  • 12
0

the above answer is right but the easiest way to do this is just name your form field name like your pojo variable name for example

<input type="text" name="loginfo.username" />

and of your right the following code in controller then that should be work..

@RequestMapping(value="yourMapping",method=RequestMethod.POST)
public String yourMethodName(PersonalInfo info){
    return "yourMpaaing";
} 
theoctober19th
  • 364
  • 3
  • 13
torikraju
  • 7
  • 2
  • 6