Front-end jsp code:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Script src = "// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Input"). focus (function (){
$ ("Input" ).css ("background-color", "# FFFFCC ");
});
$ ("Input"). blur (function (){
Var username = $. trim ($ ("# username"). val (); // $. trim remove leading and trailing spaces,. val () Get username value
$. Post ("checkusername. action", {username: username}, function (date) {// date get the returned object
If (date. result = "OK "){
$ ("# Span1" ).html ("user name can be used ");
} Else {
$ ("# Span1" ).html ("the user name cannot be used ");
}
Return false ;},
'Json ');
});
});
</Script>
</Head>
<Body>
Username: <input type = "text" name = "username" size = "20" id = "username">
<Span id = "span1"> </span>
</Body>
</Html>
Web. xml: added struts support
<Display-name> Struts2 </display-name>
<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class> org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
</Welcome-file-list>
Struts. xml: // configure the class corresponding to the action
<Package name = "struts2_login" extends = "json-default">
<Action name = "checkusername"
Class = "action. CheckUsername" method = "CheckPerson">
<Result type = "json"> </result>
</Action>
</Package>
Backend code:
Public class CheckUsername extends ActionSupport {
Private String username;
Private String result;
... Omitted set (), get ();
Public String CheckPerson () throws Exception {// perform verification logic processing. If I check that the user name is aa, OK is returned;
If (username. equals ("aa ")){
This. result = "OK ";
}
Else {
This. result = "no ";
}
System. out. println (result );
Return SUCCESS;
}
}