struts2+jquery implementation ajax login example details

  • 2020-12-19 21:08:57
  • OfStack

An example of text instrument describes the implementation method of struts2+jquery to realize ajax landing. The specific steps are as follows:

1. Create a new web project and name it test. Configure the environment for struts2 and import the js file for Jquery into the project.

2. Under com. action package, create a new ES15en. java

The code for loginAction. java 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 = { "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(" The user name cannot be empty ");
        return SUCCESS;
      } else if (getPassword() == null || getPassword().trim().equals("")) {
        setFlag(" The password cannot be empty ");
        return SUCCESS;
      } else if (getUsername().trim().equals("admin")
          && getPassword().equals("admin")) {
        setFlag(" Log in successfully ");
        return SUCCESS;
      } else {
        setFlag(" Incorrect user name or password ");
        return SUCCESS;
      }
    } catch (Exception e) {
      e.printStackTrace();
      setFlag(" Log in abnormal ");
      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 index.jsp and success.jsp in the WebRoot directory

index. jsp is the login interface, and ES33en. jsp is the interface to jump after successful login.

index.jsp reads 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">
<html>
<head>
<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>
</head>
<script type="text/javascript">
  $(document).ready(function() {
    $(".loginButton").click(function() {
      var name = $("#username").val();// Gets the 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==" Log in successfully "){
      location.href="./success.jsp" rel="external nofollow" ;
      }else{
       alert(value);
    }
  });
</script>
<body>
  <input name="username" id="username" type="text" />
  <input name="password" id="password" type="password" />
  <button class="loginButton" class="btn btn-inverse"> landing </button>
</body>
</html>

Related articles: