English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Problem Description: When using form values, if properties such as name, address, age, etc., are defined, then the values passed through the form are only these3properties, but we need the name, pass, address, age, etc., four properties. Then, when submitting the form and assigning values, there will be cases where the unobtained values are null. As shown in the following figure:
The obtained pass is null
Then we need to use the @ModelAttribute annotation to solve it:
The implementation code is as follows:
@ModelAttribute public void getUsers(@RequestParam(value="name",required=false) String name,Map<String, Object> map){ if(name!=null){ System.out.println("Calling ModelAttribute<quote>); //Simulate an object obtained from the database. User users = new User("cjh","123","[email protected]","China<quote>); System.out.println("Obtaining an object from the database<quote>);+users); map.put("user", users); } } @RequestMapping("/getInfo) public String getServletAPI(User user){ String viewName = "hello"; System.out.println("Modification:")+user); return viewName; }
The method annotated with @ModelAttribute, no matter which method is called in this controller, will be executed.
1.Firstly, execute the method decorated with @ModelAttribute annotation, find the corresponding object to be modified from the database, put the value in the map key-value pair, the key should be the same as the parameter name passed to the method handling the request, such as: here, the input parameter name is defined as user, then map.put(
2.Spring MVC will find the user object from the Map, and assign the form request parameters to the user object. Only the properties defined in the form will be replaced, and the undefined ones will be null, without change. Therefore, the name of the key defined in the method decorated with @ModelAttribute must be consistent with the name of the input parameter in the controller method (@RequestMapping() decorated method). Otherwise, it will not take effect.
3.Spring MVC passes the above object to the target method parameter.
Note: The keys in the map in the method decorated with @ModelAttribute must be consistent with the parameter names of the target method type.
Final effect:
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to support the Yelling Tutorial more!
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content artificially, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.