1. Struts. xml settings
<Package name = "default" namespace = "/" extends = "JSON-Default"> <! -- Use ajax to request this extends -->
<Action name = "userjson_checkname" class = "ssh. Action. useraction" method = "checkname">
<Result type = "JSON">
<! -- The root value corresponds to the property of the value to be returned. The result value here is the property result in the corresponding action, that is, the returned data in Ajax -->
<Param name = "root"> checkresult </param>
</Result>
</Action>
</Package>
2. html and JS. The form page to be submitted contains two JS files: jquery. js (download a version at Will) and username_check.js (as shown below)
<Form ID = "tea_reg" method = "Post" name = "tea_reg"> User Name <input name = "user. user_logname "type =" text "id =" user. user_logname "size =" 20 "maxlength =" 255 "type =" codeph "text =" codeph "/>
<Label id = "message"> verify the user name </label>
</Form>
VaR user = {inintevent: function () {$ ("input [name = 'user. user_logname'] "). unbind ("Blur");/* the user will respond when the input box is moved. user_logname is the name of the input box */$ ("input [name = 'user. user_logname'] "). BIND ("Blur", function () {user. checkuser ($ (this ). val () ;}}, checkuser: function (LOGNAME) {VaR parameter = {LOGNAME: LOGNAME/* input parameter. Why is this representation used? */}; $. Post ("userjson_checkname.action", parameter, function (data) {/* userjson_checkname indicates the action name * // * The parameter date here is in struct. the checkresult parameter defined in XML, that is, an attribute value of action */If (Data = "OK") {$ ("# message "). text ("this user name is available"); $ ("# message" ).css ("color", "blue");} else {$ ("# message "). text ("this user name is unavailable"); $ ("# message" ).css ("color", "Red ");}});}}; /* bind the action binding when starting the page */$ (document ). ready (function () {user. inintevent ();/* bind to user name input box */});
3. In the action section, add the following code to the useraction class:
Public String LOGNAME; Public String checkresult; Public String getcheckresult () {/* names must be consistent and are used to return data */return this. checkresult;} public void setlogname (string LOGNAME)/* The passed parameter LOGNAME */{This. LOGNAME = LOGNAME;} Public String getlogname () {return this. LOGNAME;} Public String checkname () {user TMP; TMP = userservice. finduserbyname (LOGNAME);/* the specific method of userservice is defined according to your own project settings, which has nothing to do with Ajax */If ( Null! = TMP) {This. checkresult = "Err";/* The final value to be returned. The browser automatically obtains */} else {This. checkresult = "OK";} return success ;}
4. Implement debugging and view the submitted and returned results in chrome.
Use SSH + ajax to check Duplicate User Names