Springcontroller:
[Java]View Plaincopy
- @Controller
- @RequestMapping ("/user")
- Public Usercontroller extends basecontroller{
- @RequestMapping ("/adduser")
- public void Testbinderouput (@ModelAttribute User user, httpservletrequest request, HttpServletResponse Response) {
- SYSTEM.OUT.PRINTLN (user);
- }
- }
Use object:
[Java]View Plaincopy
- Public Class user{
- private String name;
- private int sex;
- private String address;
- private int id;
- public int Getsex () {
- return sex;
- }
- public void setsex (int sex) {
- this.sex = sex;
- }
- Public String getaddress () {
- return address;
- }
- public void setaddress (String address) {
- this.address = address;
- }
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
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
- @Controller
- @RequestMapping ("/user")
- Public Usercontroller extends basecontroller{
- @InitBinder ("manager")
- public void InitBinder1 (Webdatabinder binder) {
- Binder.setfielddefaultprefix ("manager.");
- }
- @InitBinder ("user")
- public void InitBinder2 (Webdatabinder binder) {
- Binder.setfielddefaultprefix ("user.");
- }
- @RequestMapping ("/adduser")
- public void Testbinderouput (@ModelAttribute User user, httpservletrequest request, HttpServletResponse Response) {
- System.out.println (User.getname);
- }
- @RequestMapping ("/addmanager")
- public void Testbinderouput (@ModelAttribute Manager manager, HttpServletRequest request, HttpServletResponse response) {
- System.out.println (Manager.getname);
- }
- }
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