欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2+jquery实现ajax登陆实例详解

程序员文章站 2023-11-07 18:33:40
文本仪一个实例讲述了struts2+jquery实现ajax登陆的实现方法,具体步骤如下: 一、新建一个web项目,取名test。配置好struts2的环境,并导入jqu...

文本仪一个实例讲述了struts2+jquery实现ajax登陆的实现方法,具体步骤如下:

一、新建一个web项目,取名test。配置好struts2的环境,并导入jquery的js文件到该项目。

二、在com.action包下,新建一个loginaction.java

loginaction.java的代码如下

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("用户名不能为空");
        return success;
      } else if (getpassword() == null || getpassword().trim().equals("")) {
        setflag("密码不能为空");
        return success;
      } else if (getusername().trim().equals("admin")
          && getpassword().equals("admin")) {
        setflag("登陆成功");
        return success;
      } else {
        setflag("用户名或密码错误");
        return success;
      }
    } catch (exception e) {
      e.printstacktrace();
      setflag("登陆异常");
      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;
  }

}

三、在webroot目录下新建index.jsp和success.jsp

index.jsp为登陆界面,success.jsp为登陆成功后跳转的界面。

index.jsp内容如下:

<%@ 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();//获取登陆用户名
      var password = $("#password").val(); //获取登陆密码
      $.post("login", {
        username : name,
        password : password
      }, callback, "json");
    });

    function callback(data) {
      var value=data.flag;
      if(value=="登陆成功"){
      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">登陆</button>
</body>
</html>

上一篇: 晚上

下一篇: Vue CLI3 开启gzip压缩