Struts2+jquery realize Ajax Login instance detailed _jsp programming

Source: Internet
Author: User

Text instrument An example of the implementation of struts2+jquery implementation of Ajax landing method, the following steps:

First, create a new Web project, name test. Configure the STRUTS2 environment and import jquery's JS files to the project.

Second, under the Com.action package, a new Loginaction.java

Loginaction.java's code is as follows

Package com.action;
Import org.apache.struts2.convention.annotation.Action;
Import Org.apache.struts2.convention.annotation.ParentPackage;
Import Org.apache.struts2.convention.annotation.Result;

Import Org.apache.struts2.convention.annotation.Results;

Import Com.opensymphony.xwork2.ActionSupport; @Action ("Login") @ParentPackage (value = "Json-default") @Results ({@Result (name = "Success", type = "json", params = {"Da Ta "," Flag "}), public class Loginaction extends Actionsupport {/** * */private static final long Serialv
  Ersionuid = 1751244794407005783L;
  Private String flag;
  Private String username;

  private String password; Public String Execute () {try {if (getusername () = null | | getusername (). Trim (). Equals ("")) {Setflag (
        "User name cannot be empty");
      return SUCCESS;
        else if (getpassword () = null | | GetPassword (). Trim (). Equals ("")) {Setflag ("Password cannot be blank");
      return SUCCESS;
else if (getusername (). Trim (). Equals ("admin")          && GetPassword (). Equals ("admin")) {Setflag ("login succeeded");
      return SUCCESS;
        else {setflag ("User name or password error");
      return SUCCESS;
      } catch (Exception e) {e.printstacktrace ();
      Setflag ("Landing anomaly");
    return SUCCESS;
  } public String Getflag () {return flag;
  } public void Setflag (String flag) {This.flag = flag;
  Public String GetPassword () {return password;
  } public void SetPassword (String password) {this.password = password;
  Public String GetUserName () {return username;
  } public void Setusername (String username) {this.username = username;

 }

}

Iii. new index.jsp and success.jsp in the Webroot catalogue

Index.jsp for landing interface, success.jsp for landing after the successful jump interface.

Index.jsp contents are as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath ()
  ;  String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path
+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  
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.