Springmvc the same field parameter delivery solution for multiple objects, in Springmvc, it is sometimes necessary to pass multiple objects (except model and web elements)
As we all know, in Springmvc, the struts2-like point syntax is not supported, so it is inevitable that multiple objects with the same name can cause some problems.
Springmvc's parameter passing is very intelligent and can automatically assemble parameters to objects
public class user{ private string name; private string id; private string gender, private string age; //getter and Setter}--------- -------------------------public class food{ private string name; private string id; private string price, private string date; //getter and setter}-------<form method= ' post ' action= ' URL ' > user name <input type= ' text ' name= ' Name ' > user id <input type= ' text ' name= ' id ' > Food name <input type= ' text ' name = ' name ' > Food id <input type= ' text ' name= ' id ' > <input type= ' text ' name= ' Gender ' > <input type= ' text ' name= ' age ' > <input type= ' text ' name= ' Price ' > <input type= ' text ' name= ' date ' ></form>--------@requestMap (value={'/order /book '}) public strIng show (user u,food f) {}
In the above case, the user and food can not get the correct name and ID, or more can not get, this if struts2 is more convenient, directly using the point syntax u.name,u.id,f.name,f.id ....
In order to solve the above problems, there are 2 programs:
the first is to build a DTO
The advantage is that can be considered to avoid the transfer of parameters, directly to the DTO field name U_name,u_id,f_name,f_id,gender,age,date,price are listed, and then do the following
Then modify the form fields and Contoller
<form method= ' post ' action= ' URL ' > user name <input type= ' text ' name= ' u_name ' > User ID <input type= ' text ' name= ' u_ ID ' > Food name <input type= ' text ' name= ' f_name ' > Food ID <input type= ' text ' name= ' f_id ' > <input type= ' text ' name = ' gender ' > <input type= ' text ' name= ' age ' > <input type= ' text ' name= ' price ' > <input type= ' text ' name= ' Date ' ></form> @requestMap (value={'/order/book '}) public string Show (Userfooddto dto) {//Then split, field user U = new User (); Food f = new food (); U.setname (Dto.getuname ()); F.setname (Dto.getname ()); U.setuid (Dto.getuid ()); F.setfid (Dto.getfid ( ); U.setage (dto.getage); F.setprice (Dto.getprice);
The disadvantage is: if the amount of data is large, 10,000 fields, modify the more natural place, and a DTO, split is also very laborious, so it is not recommended to use in the case of large amounts of data
2. Use bridge connections to encapsulate different parts into one object
Advantages: reduced conflict and reduced workload
Cons: Conflicting fields/total fields approaching 1 o'clock is not necessary (however, this is rarely the case, unless it is an inheritance relationship, but the inheritance can use the subclass to pass the value, so this problem you have not encountered)
The form is as follows
<form method= ' post ' action= ' URL ' > user name <input type= ' text ' name= ' u_name ' > User id <input type= ' text ' name= ' u_id ' > Food name <input type= ' text ' name= ' f_name ' > Food id <input type= ' text ' name= ' f_id ' > <input type= ' text ' name= ' Gender ' > <input type= ' text ' name= ' age ' > <input type= ' text ' name= ' Price ' > <input type= ' text ' name= ' date ' ></form>-----To create a conflicting field specifically for a javabeanpublic String UFBridge{ private String uname; private String uid; private String fname; private string fid;} --------------------------@requestMap (value={'/order/book '}) public string show (User u,food &NBSP;F,UFBRIDGE&NBSP;UFB) { u.setname (Ufb.getuname ()); f. SetName (Ufb.getuname ()); u.setid (Ufb.getuid); f.setid ( UFB.GETFID);}
Try doing it
SPRINGMVC the best solution for passing the same field parameters to multiple objects