For beginners, learning Ajax to verify the user's unique combat is to consolidate Ajax,jquery,json and Struts2 basic knowledge, specific content as follows
Browse Effects Chart:
So we'll start with the following steps :
1. First, we first import the required packages and documents
Json Package:
STRUTS2 Package:
Introduce the Jquery-2.1.1.min.js file.
<script type= "Text/javascript" src= "Js/jquery-2.1.1.min.js" ></script>
2. Then, we need to configure the STRUTS2 filter in the Web.xml file
<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>
3. Then, start to write our login.jsp page, of course, you can add some CSS style to make it more beautiful. (I just did it a little bit low)
4. Then we started to write the action.
Verifies the uniqueness of the user public
void Isexist () throws IOException {
Boolean exist = Name.equals ("John");
Jsonobject result=new jsonobject ();
if (exist) {
result.put ("exist", true);
} else{
result.put ("exist", false);
Servletactioncontext.getresponse (). setContentType ("Text/html;charset=utf-8");
PrintWriter out=servletactioncontext.getresponse (). getwriter ();
Out.println (Result.tostring ());
Out.flush ();
Out.close ();
}
5. Finally, the preparation of struts.xml documents
<package name= "User" namespace= "/" extends= Struts-default "> <action name=" user_* "
class=" Com.ddge.action.UserAction "method=" {1} ">
<result name=" error ">/login.jsp</result>
< /action>
</package>
Summarize
Start deployment, test! The effect is not just out.
The above is the entire content of this article, I hope to help you learn.