@RequestMapping("/form")
public String form(Model model, Integer id)
For example ,spring can know parameter id's name is id and bind request param's value to it in runtime
This is a feature of javac introduced in JDK 8. You need to include -parameters
javac command line option to activate it. Then you will be able to get parameter names like this:
String name = String.class.getMethod("substring", int.class).getParameters()[0].getName()
System.out.println(name);
From Spring documentation 3.3 Java 8 (as well as 6 and 7)
You can also use Java 8’s parameter name discovery (based on the -parameters compiler flag) as an alternative to compiling your code with debug information enabled.
As specified in the javadoc for ParameterNameDiscoverer class:
Parameter name discovery is not always possible, but various strategies are available to try, such as looking for debug information that may have been emitted at compile time, and looking for argname annotation values optionally accompanying AspectJ annotated methods.
This is the link to related JIRA item in Spring project Java 8 parameter name discovery for methods and constructors
JDK Enhancement Proposal JEP 118: Access to Parameter Names at Runtime
It's the WebDataBinder
who does it for you.
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/WebDataBinder.html
As you can see on the api, WebDataBinder is for data binding from web request parameters to JavaBean objects.
WebDataBinder is also responsible for validation and conversion, so that's why if you have customized editor or validator, you have to add them to your webDataBinder in the controller like below.
@Controller
public class HelloController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(yourValidator);
binder.registerCustomEditor(the clazz to conver to.class, "the name of the parameter", yourEditor);
}
...
for more information, you have to check the document
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With