2

I have controller like below

@RequestMapping("/hello")
public void callHello(MyObject myObject) {
    //code
}

Here MyObject is a POJO class with getter and setter

public class MyObj {
    private String fName;
    private String lName;
    //getter and setter
}

I am calling this controller via ajax call as

var jqxhr = $.ajax({
    url:'hello?fName=testFname&lName=testLname',
    type:"GET",
    dataType:'JSON'
}});

This is working fine but I am wondering what is the feature of spring that automatically maps url parameter fname and lName and to field of myObject.

1 Answer 1

3

Behind the scene, there is a WebDataBinder that does the job. According to the documentation, this is a:

Special DataBinder for data binding from web request parameters to JavaBean objects.

They are initialized with a WebBindingInitializer. You can also create your own ConfigurableWebBindingInitializer.

According to the Spring Boot Reference Guide:

Spring MVC uses a WebBindingInitializer to initialize a WebDataBinder for a particular request. If you create your own ConfigurableWebBindingInitializer @Bean, Spring Boot automatically configures Spring MVC to use it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Ortomala. Can you please help me understand if @ModelAttribute is no more needed in controller.
@user8710021 Can you please ask another complete question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.