struts2+jquery實現ajax登陸執行個體詳解_JSP編程

來源:互聯網
上載者:User

文本儀一個執行個體講述了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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.