zoukankan      html  css  js  c++  java
  • struts2+jquery 实现ajax登陆

    一、新建一个web项目:test,配置好struts2的环境(详细配置见:http://www.cnblogs.com/wuweidu/p/3841297.html)

          导入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%>">
    
    <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";
                }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>

  • 相关阅读:
    模板的一些概念和技巧
    [转] Linux TCP/IP网络小课堂:net-tools与iproute2大比较
    [转] boost库的Singleton的实现以及static成员的初始化问题
    static对象的高级用法
    const中的一些tricky的地方
    delphi软件启动的顺序解密。
    属性名、变量名与 内部关键字 重名 加&
    delphi Inc函数和Dec函数的用法
    Centos 关闭防火墙
    IntelliJ IDEA 启动方法
  • 原文地址:https://www.cnblogs.com/wuweidu/p/3841715.html
Copyright © 2011-2022 走看看