Use SSH + ajax to check Duplicate User Names

Source: Internet
Author: User

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

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.