【communication】ajax之jquery与struts2通信 小实验

先创建一action,接受用户的登录参数,若用户名、密码相同,则返回success.jsp,否则返回fail.jsp

1、LoginAction.java

package com.jing.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private String name;
    private String password;
    @Override
    public String execute() throws Exception {
        if (getName().equals(getPassword())) {
            return "success";
        }else {
            return "fail";
        }
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

2struts.xml:

<package name="login" namespace="/"  extends="struts-default">
    <action name="login" class="com.jing.action.LoginAction">
        <result name="success">success.jsp</result>
        <result name="fail">fail.jsp</result>
    </action>
</package>

3、其他文件:web.xml struts.xml  success.jsp fail.jsp

至此,后台完成!!

===================================================


前台:用easyUI编写的:1.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html,charset=UTF-8" />
        <script type="text/javascript" src="./js/jquery-1.8.0.min.js"
            charset="UTF-8"></script>
        <script type="text/javascript" src="./js/jquery.easyui.min.js"></script>
        <link rel="stylesheet" type="text/css"
            href="./js/themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="./js/themes/icon.css">
        <script type="text/javascript" src="./js/locale/easyui-lang-zh_CN.js"></script>
        <link rel="stylesheet" type="text/css" href="./js/demo/demo.css">
        <script type="text/javascript">
    var loginAndRegister;

    $(function() {
        loginAndRegister = $('#loginAndRegister').dialog( {
            closable : false,
            modal : true,
            buttons : [ {
                text : '注册',
                handler : function() {
                    //console.info("注册事件");
            }
            }, {
                text : '登录',
                handler : function() {
                    //console.info("登录事件");
                $.ajax( {
                    url : 'login.action',
                    data : {
                        name : $('#loginAndRegisterForm input[name=name]').val(),
                        password : $('#loginAndRegisterForm input[name=password]').val()
                    },
                    cache : false,
                    dataType : 'json',
                    success : function(r){
                        console.info(r.msg);
                    }
                });
            }
            } ]
        });
    });
</script>
    </head>
    <body>
        <div id="loginAndRegister" title="用户登录"
            style="width: 400px; height: 200px">
            <form id="loginAndRegisterForm">
                <table align="center">
                    <tr>
                        <th align="right">
                            用户名
                        </th>
                        <td>
                            <input name="name">
                        </td>
                    </tr>
                    <tr>
                        <th align="right">
                            密码
                        </th>
                        <td>
                            <input name="password" type="password" />
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>
===================================================

运行结果:

输入:http://localhost:8080/easyUI/1.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值