Page effects
When the mouse loses focus, it does not need to refresh the page to verify that the login name is duplicated.
Implementation Step 1. Introducing the struts2-json-plugin-2.5.10.1 plug-in package
2. The page uses jquery Ajax to implement the background check JS
/** Verify that the login name is duplicated*/ functionCheckUser (o) {//alert (o.value);//dom's notation //Alert ($ (O). Val ());//jquery varLogonName =$ (O). Val (); //query If the login has a record in the database table as a query condition $.post ("Elecuseraction_checkuser.do", {"LogonName": Logonname},function (data){//if the top of the stack is a model-driven object, you should use the Data.message method when you take the value //if the top of the stack is a property of a model-driven object, you should use data when you take the value if(data==1){ $("#check"). HTML ("<font color= ' red ' > Login name cannot be empty </font>"); $ (o) [0].focus (); $("#BT_Submit"). attr ("Disabled", "none"); } Else if(data==2){ $("#check"). HTML ("<font color= ' red ' > Login already exists </font>"); $ (o) [0].focus (); $("#BT_Submit"). attr ("Disabled", "none"); } Else{ $("#check"). HTML ("<font color= ' green ' > login name can be used </font>"); $("#BT_Submit"). attr ("Disabled", "" "); } }); }
You can see the data as an identifier to determine if the login is duplicated, so add the non-persisted JavaBean attribute to the entity class Elecuser.java
// checks if the login name duplicates the identifier Private String message; Public String getMessage () { return message;} Public void setmessage (String message) { this. Message = message;}
3. Define checkuser () in the Code of the Action class: Place a message of type string to the top of the stack
/*** @Name: CheckUser * @Description: Use jquery ajax to complete the background check of the login name, determine if the database exists, and ensure that the login name is unique * @Parameters: no * @Retur N: JSON plug-in package with Struts2*/ PublicString CheckUser () {//1: Get Login nameString LogonName =Elecuser.getlogonname (); //2: Determine if the login name is duplicatedString message =Elecuserservice.checkuser (LogonName); //place to top of stackElecuser.setmessage (message);//the top object of the stack is the Elecuser object//valueutils.putvaluestack (message);//top of Stack object is a String type property return"CheckUser"; }
This invokes the service class's CheckUser (LogonName) method, gets the value of the identifier, and then puts it on top of the stack.
4. Querying the value of the message identity in the service class's Code
/*** @Name: CheckUser * @Description: Determine if the login name exists * @Parameters: LogonName login * @Return: String: Determine if the login name duplicates , returns an Identity message property * Message=1 indicates that the login name is empty and cannot be saved * message=2 indicates that the login exists and cannot be saved * MESSAG E=3 indicates that the login name does not exist and can be saved*/@Override Publicstring CheckUser (String logonname) {string Message=""; if(Stringutils.isnotblank (LogonName)) {//ToLogin as query criteria, query databaseString condition= "and o.logonname =?"; Object[] Params={LogonName}; List<ElecUser> list = Elecuserdao.findcollectionbyconditionnopage (condition, params,NULL); //indicates that the login name exists and cannot be saved if(list!=NULL&&list.size () >0) {Message= "2"; } //indicates that the login name does not exist and can be saved Else{message= "3"; } } //logon name is empty Else{message= "1"; } returnmessage; }
5. Add in Struts.xml
<resultname= "CheckUser"type= "JSON"> <!--root Specifies that the message indicates if there is a message property on the top of the stack, and if so, gets the value to the top of the stack directly through the Get method of the message - <paramname= "root">Message</param> <!--a property of the object is JSON - <paramname= "Includeproperties">Message</param></result>
Here, the name root of the parameter indicates that if the identifier message is placed in a Elecuser object, and the top of the stack is a Elecuser object, the message property value is placed directly through the Get method of the message to the top of the stack. The page JS does not need to use Data.message to take the value. You can see the struts plug-in package source code specifically:
protectedObject Findrootobject (actioninvocation invocation) {object rootobject;//find if the top of the stack has a message property if( This. root! =NULL) {//if the object has a message property, get the value directly through the Get method of the message and put it on top of the stackValuestack stack =Invocation.getstack (); Rootobject=Stack.findvalue (root); } Else{rootobject= Invocation.getstack (). Peek ();//model overrides Action } returnRootobject; } protectedString createjsonstring (httpservletrequest request, Object Rootobject)throwsjsonexception {String json=jsonutil.serialize (Rootobject, Excludeproperties, Includeproperties, Ignorehierarchy, Enumasbean, Excludenullproperties, Defaultdateformat); JSON=addcallbackifapplicable (request, JSON); returnJSON; }
User-managed design--4.jquery The AJAX implementation of the login name checksum