I've never been able to understand it before. struts2+jquery+ implementation Ajax No refresh authentication user name How to do, and then after efforts, finally fix, should pay attention to the content of struts configuration file, good directly on the code
Page code
<Body> <S:formAction= "Registaction"Method= "POST"namespace= "/user"> <Table> <TR> <TD>UserName</TD> <TDID= "T1"><inputtype= "text"name= "UserName"ID= "UserName" /></TD> <TD><inputtype= "button"ID= "Test"value= "Test" /><DivID= "Tip1"></Div></TD> </TR> <TR> <TD>Password</TD> <TDID= "T2"colspan= "2"><inputtype= "Password"name= "UserPwd1"ID= "UserPwd1" /></TD> </TR> <TR> <TD>Password AG</TD> <TDID= "T3"colspan= "2"><inputtype= "Password"name= "UserPwd2"ID= "UserPwd2" /></TD> </TR> <TR> <TDcolspan= "3"> <inputtype= "Submit"value= "Submit" /> <inputtype= "Reset"value= "Reset" /> </TD> </TR> </Table> </S:form> </Body>
JavaScript code:
$ (document). Ready (function(){ varInputusernameobj = $ ("#userName"); varInputuserpwd1obj = $ ("#userPwd1"); varInputuserpwd2obj = $ ("#userPwd2"); $("#userName"). Blur (function(){ varText =Inputusernameobj.val (); $.post ("Testaction.action?username=" +text,NULL,function(response) {if(response== "User name already exists") {document.getElementById ("Tip1"). Innerhtml= "<font color= ' Red ' >" +response+ "</font>"; }Else{document.getElementById ("Tip1"). Innerhtml= "<font color= ' Blue ' >" +response+ "</font>"; } }); }); });
Action Code:
Public classTestactionextendsactionsupport{HttpServletRequest HSR; String result; Publichttpservletrequest GETHSR () {returnHSR; } Public voidSETHSR (HttpServletRequest HSR) { This. HSR =HSR; } PublicString GetResult () {returnresult; } Public voidSetresult (String result) { This. result =result; } PublicString Test ()throwsioexception{Map<string, string> map =NewHashmap<string, string>(); Actioncontext AC=Actioncontext.getcontext (); HttpServletRequest req=(HttpServletRequest) ac.get (servletactioncontext.http_request); HttpServletResponse Res=(HttpServletResponse) ac.get (servletactioncontext.http_response); String UserName= Req.getparameter ("UserName"); Map.put ("UserName", UserName); Res.setcontenttype ("Text/html;charset=utf-8"); PrintWriter W=Res.getwriter (); if("Jack". Equals (UserName)) { //JSON processing of the map object that will be returnedJsonobject Jo =jsonobject.fromobject (map); System.out.println ("Into the ..."); //call the JSON object's ToString method to convert to a string and assign a value to result This. result =jo.tostring (); W.println ("User name already exists"); }Else{w.println ("User name does not exist"); } w.close (); return"Success"; } }
Struts configuration:
< Packagename= "Ajax"extends= "Json-default" > <!--Verify that the user name exists - <Actionname= "Testaction"class= "Com.hisoft.jqt.action.TestAction"Method= "Test"> <!--The return type is json defined in Sjon-default - <resulttype= "JSON"name= "Success"> <!--The root value corresponds to the property of the value to be returned - <!--The result value here is the result in the corresponding action - <paramname= "root">Result</param> </result> </Action> </ Package>