Detailed explanation of Ajax login instance based on struts 2 + jquery


An example of text instrument describes the implementation method of struts 2 + jQuery to realize Ajax login. The specific steps are as follows:

1、 Create a new web project named test. Configure the Struts2 environment, and import the JS file of jQuery to the project.

2、 In com.action Package, create a new one The code for 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;

@ParentPackage(value = "json-default")
@Results({ @Result(name = "success", type = "json", params = { "data", "flag" }), })
public class LoginAction extends ActionSupport {

  private static final long serialVersionUID = 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 empty");
        return SUCCESS;
      } else if (getUsername().trim().equals("admin")
          && getPassword().equals("admin")) {
        Setflag ("login succeeded");
        return SUCCESS;
      } else {
        Setflag ("wrong user name or password");
        return SUCCESS;
    } catch (Exception e) {
      Setflag ("login exception");
      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;


3、 Create a new one in the Webroot directory index.jsp and success.jsp

index.jsp Is the login interface, success.jsp It is the interface to jump after successful login.

index.jsp The 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">
<base href="<%=basePath%>" rel="external nofollow" >

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="./resource/js/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $(".loginButton").click(function() {
      Var name = $("#username"). Val(); // get login user name
      Var password = $("špassword"). Val(); // get the login password
      $.post("login", {
        username : name,
        password : password
      }, callback, "json");

    function callback(data) {
      var value=data.flag;
      If (value = = login succeeded){
      location.href="./success.jsp" rel="external nofollow" ;
  <input name="username" type="text" />
  <input name="password" type="password" />
  < button > login

Recommended Today

IOS dyld loading process (preliminary discussion)

Compilation process of IOS app code source file: load h、. m、. CPP and other documents Pretreatment: replace macros, delete comments, expand header files, and generate I file compile: will I file is converted into assembly language to generate S file assembly: convert assembly file into machine code file to generate O documentation link: Yes O […]