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

相關文章
相關標籤/搜索