Ajax verifies the uniqueness of the user _ajax related

Source: Internet
Author: User

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)

<div class= "Row" >
  
 

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.

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.