Ajax verifies user uniqueness and Ajax verifies user
For beginners, learning about the uniqueness of Ajax verification users is to consolidate the basic knowledge of Ajax, Jquery, Json, and Struts2. The details are as follows:
Browsing:
Then we started,ProcedureAs follows:
1. First, we need to import the required packages and files.
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, write our login. jsp page. Of course, you can add some CSS styles to it to make it more beautiful. (I just did it, a little Low)
<Div class = "row">
4. Then, we begin to write the Action.
// Verify the uniqueness of the user. public void isexist () throws IOException {boolean exist = name. equals ("Zhang San"); 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, compile the Struts. xml file.
<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>
Summary
Start deployment and test! The effect is out.
The above is all the content of this article, hoping to help you learn.