jquery Ajax verifies that a user name exists (background spring MVC)

Source: Internet
Author: User

Controller layer

  = "  /user/isexist   ", produces =  " application/json  "  )  public   Boolean isexist (model model, String UserName {boolean flag  = Userprivilegeservicefacade. Isexi    Stusername (UserName);  if   return  true  ;  return  false  ;}

JSP page

<form:formclass="Form-horizontal"Id="form"Method="Post"Modelattribute="User"enctype="Multipart/form-data"> <divclass="Control-group"> <form:label path="UserName" class="Control-label"> <spring:message code="System.user.userName"/> </form:label> <divclass="Controls"> <form:input path="UserName"Onblur="checkisexist ();"onfocus="clearcss ();"/> <site:required/> <span id="Showresult"></span> <form-extension:errors path="UserName"cssclass="Error"/> </div> </div></form:form>
<script type="Text/javascript">function Checkisexist () {varUserName = $.trim ($ ("#userName"). Val ()); $.ajax ({type:"POST",//HTTP request ModeUrl:"<spring:url value="/system/userprivilege/user/isexist"/>",//URL sent to serverData"username="+username,//parameters sent to the serverDataType:"JSON",//tell jquery the data format returned (note that the data format must be consistent with the data format returned by the submitted controller, otherwise the callback function will not be called complete)complete:function (msg) {if(Eval ("("+ Msg.responsetext +")")) {                    $("#showResult"). HTML ("<font color= ' Red ' > username already exists </font>"); } Else {                    $("#showResult"). HTML (""); }            }//The callback function is called when the interaction is defined and the server returns data correctly        }); } function Clearcss () {$ ("#showResult"). HTML (""); }    </script>

Example of error (Controller layer)

" /user/isexist " " Application/json "   public boolean isexist (Model model,string userName) {        =  Userprivilegeservicefacade             . Isexistusername (userName);             return Flag;}

Examples of optimizations

Controller layer

@ResponseBody @requestmapping (value="/user/isexist", produces ="Application/json")  Publicresultdto isexist (Model model,string userName) {resultdto result=Newresultdto (); Boolean Success=Userprivilegeservicefacade. Isexistusername (UserName);            Result.setsuccess (Success); if(Success) {Result.setmessage ("<font color= ' Red ' > username already exists </font>"); } Else{result.setmessage ("<font color= ' green ' > User name legal </font>"); }    returnresult;}

Domain Layer

 Public classResultdto implements serializable{Private StaticFinalLongSerialversionuid =1L; PrivateBoolean success; PrivateString message;  PublicBoolean issuccess () {returnsuccess; }     Public voidsetsuccess (Boolean success) { This. Success =success; }     PublicString getMessage () {returnmessage; }     Public voidsetmessage (String message) { This. Message =message; }}

JSP page

<script type="Text/javascript">function Checkisexist () {varUserName = $.trim ($ ("#userName"). Val ()); $.ajax ({type:"POST",//HTTP request ModeUrl:"<spring:url value="/system/userprivilege/user/isexist"/>",//URL sent to serverData"username="+username,//parameters sent to the serverDataType:"JSON",//tell jquery the data format returned (note that the data format must be consistent with the data format returned by the submitted controller, otherwise the callback function will not be called complete)complete:function (msg) {varresult = eval ("("+ Msg.responsetext +")"); if(result.success) {$ ("#showResult"). HTML (result.message); } Else {                    $("#showResult"). HTML (result.message);    }            }         }); } function Clearcss () {$ ("#showResult"). HTML (""); }    </script>

jquery Ajax verifies that a user name exists (background spring MVC)

Related Article

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.