Struts2与jQuery.ajax()的结合

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

loginAction.javaajax

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

login.jspost

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

struts.xmlurl

<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/spa