struts2+jquery+ implementation Ajax No refresh verify user name exists

Source: Internet
Author: User

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>

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.