Solve the problem of duplicate object binding parameters in spring MVC

Source: Internet
Author: User

HTML page

<formMethod= ' Post 'Action= ' url '>User name<inputtype= ' text 'name= ' name '>User ID<inputtype= ' text 'name= ' id '>Food Name<inputtype= ' text 'name= ' name '>Food ID<inputtype= ' text 'name= ' id '>     <inputtype= ' text 'name= ' age '>     <inputtype= ' text 'name= ' Price '></form>

Entity class

 Public classuser{PrivateString name; PrivateString ID;   PrivateInteger age; //Getter and Setter}---------------------------------- Public classfood{PrivateString name; PrivateString ID; PrivateDouble Price,   //Getter and Setter}
Controller
@requestMap (value={'/order/book '}) public string Show (User U,food f) {}
in these cases, neither user nor food can get the correct name and ID, or the1. Create an intermediate bridge, split the properties

Establish an intermediate Bridge Userfooddto object

 Public classuserfooddto{PrivateString uid;//User ID    PrivateString uname;//User name    PrivateString FID;//Food ID    PrivateString fname;//Food Name    PrivateDouble Price,//Food prices    PrivateInteger age;//User Age//Getter and Setter}

Modify the name value of the foreground page

<formMethod= ' Post 'Action= ' url '>User ID<inputtype= ' text 'name= ' uid '>User name<inputtype= ' text 'name= ' uname '>Food ID<inputtype= ' text 'name= ' FID '>Food Name<inputtype= ' text 'name= ' fname '>    <inputtype= ' text 'name= ' age '>    <inputtype= ' text 'name= ' Price '></form>

Controller

@requestMapping (value={'/order/book ') public string Show (Userfooddto dto) {  // create user and food instances New  New Food ();    // Set properties individually  U.setuid (Dto.getuid ()); F.setfid (Dto.getfid ());
U.setname (Dto.getuname ());   F.setname (Dto.getfname ());  U.setage (Dto.getage);  F.setprice (Dto.getprice); .....}  

The downside is that if you have a large amount of data, 10,000 fields, a lot of modifications, and a DTO, it's laborious to split, so it's not recommended to use it in large amounts of data.

2. Splitting conflicting properties using a bridge connection

Front Page

<formMethod= ' Post 'Action= ' url '>User name<inputtype= ' text 'name= ' uname '>User ID<inputtype= ' text 'name= ' uid '>Food Name<inputtype= ' text 'name= ' fname '>Food ID<inputtype= ' text 'name= ' FID '>    <inputtype= ' text 'name= ' age '>    <inputtype= ' text 'name= ' Price '></form>

Intermediate Bridge Class

--- specifically create a JavaBean public Class ufbridge{  private  String uname for conflicting fields;   Private  String uid;   Private  String fname;  Private  String fid;}

Controller

@requestMapping (value={'/order/book '}) public string Show (User u,food F,ufbridge ufb) {  U.setid (UFB.GETUID);  U.setname (Ufb.getuname ());  F.setid (UFB.GETFID); F.setname (Ufb.getuname ());
}

3. Create a class containing user and food

Vo Object

 Public class  userfoodvo{  private  user user;   Private food food ;  // omit getter and Setter methods }

Front Page

<formMethod= ' Post 'Action= ' url '>User name<inputtype= ' text 'name= ' User.Name '>User ID<inputtype= ' text 'name= ' User.ID '>Food Name<inputtype= ' text 'name= ' Food.name '>Food ID<inputtype= ' text 'name= ' food.id '>    <inputtype= ' text 'name= ' User.age '>    <inputtype= ' text 'name= ' Food.price '></form>

Controller

@requestMapping (value={'/order/book '}) public string Show (Userfoodvo vo) {}

Solve the problem of duplicate object binding 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.