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

零到壹度
Release: 2023-03-23 08:58:02
Original
1219 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!