1、客户端是通过$.ajax()方法向login.action传递数据;
2、其中action中execute()方法返回值为空,并通过【ServletActionContext.getResponse().getWriter().print(result);】 方法将数据传到jQuery中。

loginAction.java

public String execute() throws Exception {
    boolean result = false;
    if (username.equals(password)) {
        result = true;
    }
    ServletActionContext.getResponse().getWriter().print(result);
    return null;
}

login.js

$.ajax({
    type : "post",
    url : "login.action",
    data : $("#form1").serialize(),
    success : function(data, textStatus) {
        if (data == "true") {
            alert("成功");
        }
        if (data == "false") {
            alert("失败");
        }
    }
});

struts.xml

<package name="struts2" namespace="/" extends="struts-default">
    <action name="login" class="cn.hist.water.action.LoginAction"></action>
</package>

 

参考:http://blog.163.com/asd_wll/blog/static/210310402011103001241985/

分类:

技术点:

相关文章: