0

As I read explanation here, I found that Spring can automatically bind GET request parameter to a type. Below is the sample code from the link.

@Controller
@RequestMapping("/person")
public class PersonController {
        ...             
        @RequestMapping("/create")
        public String create(Person p) {
                //TODO: add Person to DAO
                return "person/show";
        }
}

Can someone tell me how spring do this? What bean that contains the logic to convert the parameter onto command type (Person type)?

1 Answer 1

1

The trick is done here: org.springframework.web.method.annotation.ModelAttributeMethodProcessor#resolveArgument()

This is the excerpt of code where it actually binds the class to the values:

String name = ModelFactory.getNameForParameter(parameter);
//Here it determines the type of the parameter and creates an instance
Object attribute = (mavContainer.containsAttribute(name)) ?
            mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);

//Then it binds the parameters from the servlet to the previously created instance
WebDataBinder binder = binderFactory.createBinder(request, attribute, name);
if (binder.getTarget() != null) {
    bindRequestParameters(binder, request);
    validateIfApplicable(binder, parameter);
    if (binder.getBindingResult().hasErrors()) {
        if (isBindExceptionRequired(binder, parameter)) {
            throw new BindException(binder.getBindingResult());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.