为什么Struts1 ajax ActionForm值为null

时间:2018-04-27 06:18:38

标签: ajax action struts-1

在Struts1项目中,我试图发送ajax,
chrome请求有效负载很好{" account":" abcd"," pwd":" 1234"}
但是在debugMode中,actionForm的值全为空。

ajax地区:

function loging() {
        alert(getFormData());
        $.ajax({
            url : '${pageContext.request.contextPath}/hello.do?method=jsonHi',
            type : 'POST',
            data : getFormData(),
            contentType : 'application/json',
            dataType : 'json',
            async:false,
            success : function(data) {
                alert("success");
            },
            error : function() {
                alert("error!");
            }
        });
    }
function getFormData() {
        return JSON.stringify({
            'account' : $("#account").val(),
            'pwd' : $("#pwd").val()
        });
    };

struts-config区域:

<struts-config>
<form-beans>
    <form-bean name="formClass" type="com.pete.form.AccountForm" />
</form-beans>

<action-mappings>
    <action name="formClass" path="/hello" parameter="method" type="com.pete.action.HelloAction" scope="request" validate="false">
        <forward name="helloUser" path="/WEB-INF/pages/hello.jsp" />
        <forward name="jsonHi" path="/WEB-INF/pages/afterAjax.jsp"/>
    </action>
</action-mappings>

表单区域:

    public class AccountForm extends ActionForm{

    private static final long serialVersionUID = -7462505002509046403L;
    private String account = null;
    private String pwd= null;
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }   
}

行动区:

    public class HelloAction extends DispatchAction {
    public ActionForward jsonHi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        AccountForm reqForm = (AccountForm) form;
        System.out.println(reqForm.getAccount());//console is null
        System.out.println(reqForm.getPwd());// console is null
        return null;
    }

}

我不知道形式的值是什么?

1 个答案:

答案 0 :(得分:0)

我发现问题是我的ajax不是表单提交,如果我想获得价值 我必须使用HttpServletRequest getReader()