The use of Spring MVC for @initbinder data binding

Source: Internet
Author: User

Javabean

Platformuser.java

@Entity @table (name =  "Dsm_user") public class platformuser extends dsmobject  {@Id @generatedvalue (strategy = generationtype.auto) @Columnprivate  integer userid;@ columnprivate string username; @Columnprivate  String password; @Columnprivate  string  work_id; @ManyToOne (Cascade = cascadetype.merge, fetch = fetchtype.eager) @ Joincolumn (name =  "org_id") private platformorganization org_id;//  eager to load, Load the user information while loading the role information @manytomany (Fetch = fetchtype.eager) @JoinTable (name =  "Dsm_user_role",  joinColumns = {  @JoinColumn (name =  "userid")  }, inversejoincolumns  = {  @JoinColumn (name =  "rid")  }) private set<platformrole> roles  = new HashSet<> (); @Columnprivate  String telephone;public Integer  GetUserid ()  {return userid;} Public vOid setuserid (Integer userid)  {this.userid = userid;} Public string getusername ()  {return username;} Public void setusername (String username)  {this.username = username;} Public string getpassword ()  {return password;} Public void setpassword (String password)  {this.password = password;} Public set<platformrole> getroles ()  {return roles;} Public void setroles (Set<platformrole> roles)  {this.roles = roles;} PUBLIC&NBSP;STRING&NBSP;GETWORK_ID ()  {return work_id;} public void setwork_id (string work_id)  {this.work_id = work_id;} PUBLIC&NBSP;PLATFORMORGANIZATION&NBSP;GETORG_ID ()  {return org_id;} public void setorg_id (platformorganization org_id)  {this.org_id = org_id;} Public string gettelephone ()  {return telephone;} Public&nBsp;void settelephone (String telephone)  {this.telephone = telephone;} Public platformuser ()  {}}

Platformrole.java

@Entity @table (name =  "Dsm_role") public class platformrole extends dsmobject  {@Id @generatedvalue (strategy = generationtype.auto) @Columnprivate  integer role_id;@ columnprivate string role_name; @Columnprivate  Integer role_class; @ManyToMany (mappedby  =  "Roles", Fetch = fetchtype.eager) private set<platformuser> users =  new HashSet<> ();//  eager to load, at the same time the management role can load the appropriate permissions @manytomany (Fetch = fetchtype.eager) @ Jointable (name =  "Dms_role_pri", joincolumns = {  @JoinColumn (name =  " Rid ")  }, inverseJoinColumns = {  @JoinColumn (name = " pid ")  }) Private &NBSP;SET&LT;PLATFORMPRIVILEGE&GT;&NBSP;PRIS;PUBLIC&NBSP;INTEGER&NBSP;GETROLE_ID ()  {return role_id;} public void setrole_id (integer role_id)  {this.role_id = role_id;} Public string getrole_name () &NBSp {return role_name;} Public void setrole_name (String role_name)  {this.role_name = role_name;} Public integer getrole_class ()  {return role_class;} Public void setrole_class (Integer role_class)  {this.role_class = role_class;} Public platformrole (Integer role_id, string role_name, integer role_class)   {Super ();this.role_id = role_id;this.role_name = role_name;this.role_class =  Role_class;} Public platformrole ()  {}public set<platformuser> getusers ()  {return users;} Public void setusers (Set<platformuser> users)  {this.users = users;} Public set<platformprivilege> getpris ()  {return pris;} Public void setpris (Set<platformprivilege> pris)  {this.pris = pris;}}

User and role are many-to-many relationships.

Bind a user's multiple role information to a checkbox while updating a user

Controller:

@InitBinderpublic  void initbinder (Webdatabinder binder)  throws exception { Binder.registercustomeditor (Set.class, new propertyeditorsupport ()  {@Overridepublic  void  setastext (String text)  {system.out.println (text);// long systeminfoid =  long.valueof (text);// systeminfo systeminfo =// systeminfoservice.findbyid (systemInfoId); String[] roles = text.split (","); Set<platformrole> platformroles = new hashset<> (); platformrole platformrole = null;for  (int i = 0; i <  roles.length; i++)  {platformrole = platformroleservice.findrolebyid (Integer.parseInt (roles [i])); Platformroles.add (platformrole);} System.out.println (Platformrole); SetValue (platformroles);});} /*********** upduser **************/@RequestMapping (value =  "/{user_id}/update",  method =  Requestmethod.get) public string update (@PathVariable  int user_id, model model)  {model.addattribute ("Platformorganization", platformorgservice.findallorg ()); Model.addattribute ("user_id" ,  user_id); Platformuser u = platformuserservice.finduserbyid (USER_ID); Set<platformrole> us = u.getroles (); Model.addattribute (U); List<platformrole> r = platformroleservice.findallrole ();/* * for (int i=0; I<r.size (); i++) { if (R.get (i). getrole_id (). Equals (US))  } */iterator<platformrole> it = us.iterator ();while  (It.hasNext ())  { Platformrole po = it.next (); int itu = po.getrole_id ();for  (int i  = 0; i < r.size ();  i++)  {if  (R.get (i). getrole_id (). Equals (ITU))  { R.set (I,&NBSP;PO);}}} Model.addattribute ("Platformrole",  r);return  "/platform/upduser";} @RequestMapping(value =  "/{user_id}/update",  method = requestmethod.post) public String  Update (@PathVariable  int user_id, platformuser user)  {platformuserservice.updateuser ( User);return  "redirect:/platformindex/users";}

In updateuser.jsp:

<%@ page language= "java"  import= "java.util.*"  pageencoding= "UTF-8"%><%@  taglib prefix= "Form"  uri= "Http://www.springframework.org/tags/form"%><% @taglib  uri= "http ://java.sun.com/jsp/jstl/core " prefix=" C "%><%string path = request.getcontextpath (); String basepath = request.getscheme ()  +  "://" + request.getservername ()  +   ":"  + request.getserverport () + path +  "/";%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" > 


The use of Spring MVC for @initbinder data binding

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.