Home  >  Article  >  Backend Development  >  How SpringMVC solves the problem of parameter binding with the same name

How SpringMVC solves the problem of parameter binding with the same name

零到壹度
零到壹度Original
2018-04-04 17:41:451272browse

This article mainly introduces how SpringMVC solves the problem of binding parameters with the same name. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

For example, my form is like this:

<span style="font-size: 16px;"><form action="/test.action" method="post">  <br>    <input name="user.name">  <br>    <input name="acc.name">  <br>    <input type="submit">  <br></form> <br></span>

If it is sturts, this is good To solve the problem, just declare the user and acc objects in the Controller, but SpringMVC's parameter binding is different from struts. It will automatically find the corresponding property binding, and if your action is like this:

<span style="font-size: 16px;">@RequestMapping("/test.action")<br>public void test(Account account, User user){<br>    System.out.println(user);<br>    System.out.println(account);<br>} <br></span>

If this happens, an error will be reported. What should I do?

The @InitBinder annotation is used here. For a detailed explanation, you can find relevant information. Here I only talk about how to use it. Add the following two methods to the Controller class, which are used to assign the value of the specified starting identifier to the object with the specified name

<span style="font-size: 16px;">@InitBinder("account")  <br>public void initAccountBinder(WebDataBinder binder) {  <br>    binder.setFieldDefaultPrefix("acc.");  <br>} <br><br>@InitBinder("user")  <br>public void initUserBinder(WebDataBinder binder) {  <br>    binder.setFieldDefaultPrefix("user.");  <br>}<br></span>

Then change the action method to the following. .

<span style="font-size: 16px;">@RequestMapping("/test.action")<br>public void test(@ModelAttribute("account") Account account, @ModelAttribute("user") User user){<br>    System.out.println(user);<br>    System.out.println(account);<br>}<br></span>

Note that the parameters in @ModelAttribute must correspond to the values ​​in @InitBinder defined above, otherwise the value will not be obtained.

Related recommendations:

SpringMvc object binding parameter duplicate name solution

Spring MVC Parameter binding with the same name for different objects

About a reason why springmvc pojo parameter binding is "unsuccessful"

The above is the detailed content of How SpringMVC solves the problem of parameter binding with the same name. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn