


How SpringMVC solves the problem of parameter binding with the same name
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Two WeChat accounts cannot be bound to the same bank card. Bind a bank card to a WeChat account: 1. Open the WeChat application, click the "Me" option, and then select the "Pay" option; 2. Select the "Add Bank Card" option and enter the bank card information as prompted; 3. Once the bank card is successfully bound, users can use the bank card to make payments and transfers in WeChat.

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

New feature of PHP5.4 version: How to use callable type hint parameters to accept callable functions or methods Introduction: PHP5.4 version introduces a very convenient new feature - you can use callable type hint parameters to accept callable functions or methods . This new feature allows functions and methods to directly specify the corresponding callable parameters without additional checks and conversions. In this article, we will introduce the use of callable type hints and provide some code examples,

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

In today's era of information explosion, the construction of personal brand and corporate image has become increasingly important. As the leading fashion life sharing platform in China, Xiaohongshu has attracted a large number of user attention and participation. For those users who want to expand their influence and improve the efficiency of content dissemination, binding sub-accounts has become an effective means. So, how does Xiaohongshu bind a sub-account? How to check whether the account is normal? This article will answer these questions for you in detail. 1. How to bind a sub-account on Xiaohongshu? 1. Log in to your main account: First, you need to log in to your Xiaohongshu main account. 2. Open the settings menu: click "Me" in the upper right corner, and then select "Settings". 3. Enter account management: In the settings menu, find the "Account Management" or "Account Assistant" option and click

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

1. Open Toutiao. 2. Click My in the lower right corner. 3. Click [System Settings]. 4. Click [Account and Privacy Settings]. 5. Click the button on the right side of [Douyin] to bind Douyin.
