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)