How to pass object parameters in Spring MVC

Source: Internet
Author: User

Springcontroller:

[Java]View Plaincopy
  1. @Controller
  2. @RequestMapping ("/user")
  3. Public Usercontroller extends basecontroller{
  4. @RequestMapping ("/adduser")
  5. public void Testbinderouput (@ModelAttribute User user, httpservletrequest request, HttpServletResponse Response) {
  6. SYSTEM.OUT.PRINTLN (user);
  7. }
  8. }


Use object:

[Java]View Plaincopy
  1. Public Class user{
  2. private String name;
  3. private int sex;
  4. private String address;
  5. private int id;
  6. public int Getsex () {
  7. return sex;
  8. }
  9. public void setsex (int sex) {
  10. this.sex = sex;
  11. }
  12. Public String getaddress () {
  13. return address;
  14. }
  15. public void setaddress (String address) {
  16. this.address = address;
  17. }
  18. public int getId () {
  19. return ID;
  20. }
  21. public void setId (int id) {
  22. this.id = ID;
  23. }
  24. public void SetName (String name) {
  25. this.name = name;
  26. }
  27. }

Request Path:
Localhost/user/adduser?user.name= "Test"
In the parameters accepted in the background, the Name property of the user object is null. If the path is changed to Localhost/user/adduser?name= "test", the Name property of the user object is test.
This must be done with name= "test" instead of user.name= "test", because SPRINGMVC is not supported by default for User.Name this way of communicating.

If you have an object manager that has the same attribute as name, you can use User.name,manager.name to transfer parameters. But this requires adding a prefix binding to the controller:

The Controller class after adding the binding prefix is as follows:

[Java]View Plaincopy
  1. @Controller
  2. @RequestMapping ("/user")
  3. Public Usercontroller extends basecontroller{
  4. @InitBinder ("manager")
  5. public void InitBinder1 (Webdatabinder binder) {
  6. Binder.setfielddefaultprefix ("manager.");
  7. }
  8. @InitBinder ("user")
  9. public void InitBinder2 (Webdatabinder binder) {
  10. Binder.setfielddefaultprefix ("user.");
  11. }
  12. @RequestMapping ("/adduser")
  13. public void Testbinderouput (@ModelAttribute User user, httpservletrequest request, HttpServletResponse Response) {
  14. System.out.println (User.getname);
  15. }
  16. @RequestMapping ("/addmanager")
  17. public void Testbinderouput (@ModelAttribute Manager manager, HttpServletRequest request, HttpServletResponse response) {
  18. System.out.println (Manager.getname);
  19. }
  20. }

In this way, when using the connection localhost/user/adduser?user.name= "test", to request, the background gets the name parameter is not NULL.

There is a topic devoted to this issue: http://www.iteye.com/topic/1124433?page=2 can refer to the following

How to pass object parameters in Spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.