Struts 2不会将ExtJS表单中的参数传递给ActionSupport类

时间:2010-12-10 19:27:34

标签: forms extjs struts2 struts

我正在实现一个提交到Struts 2 ActionSupport类的简单ExtJS表单。各种组件的代码如下:

MyAction.java:

//packaging and imports
public class MyAction extends ActionSupport {
    private String aField;
    private String anotherField;

    public String execute() throws Exception {
        System.out.println(afield + " " + anotherField); //just checking values, atm
        return ActionSupport.SUCCESS;
    }

    public String getAField() {
        return this.aField;
    }

    public void setAField(String aField) {
        this.aField = aField;
    }

    public String getAnotherField() {
        return this.anotherField;
    }

    public void setAnotherField(String anotherField) {
        this.anotherField = anotherField;
    }
}

myForm.js:

Ext.onReady(function() {
    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var myForm = new Ext.form.FormPanel({
        id: 'myFormId',
        url: 'submitMyForm.action',
        defaults: {
            xtype: 'textfield'
        },
        items: [
            {
                fieldLabel: 'A Field',
                id: 'aField',
                name: 'aField',
                allowBlank: false
            },
            {
                fieldLabel: 'Another Field',
                id: 'anotherField',
                name: 'anotherField',
                allowBlank: false
            }
        ],
        renderTo: 'contentMain'
    });

    var submitButton = new Ext.Button({
        text: 'SUBMIT',
        handler: function(button, event) {
            myForm.getForm().submit({
                url: 'submitMyForm.action',
                failure: function() {
                    Ext.Msg.alert('Error', 'Can not save data.');
                }
            });
        }
    });
});

struts.xml中:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="myPackage" namespace="/" extends="json-default">
        <action name="submitMyForm" class="mycodepackage.MyAction">
            <result name="*" type="json">
                <param name="includeProperties">aField</param>
            </result>
        </action>
    </package>
</struts>

按下提交按钮后,我的操作正确执行,除了打印出标准调试数据外:

null null

JSON结果正确发回,但当然也是null:

14:22:17,046DEBUG JSONResult:68 - Adding include property expression:  aField
14:22:17,052DEBUG JSONWriter:68 - Ignoring property because of include rule:  anotherField
14:22:17,053DEBUG JSONUtil:68 - [JSON]{"aField":null}

现在,我的理解是,表单中输入的值应该插入到我的动作类的实例变量中。我错了吗?如果没有,可能会出现什么问题?如果是这样,我该怎么做才能确保将表单数据发送到我的操作处理程序?

感谢。

2 个答案:

答案 0 :(得分:0)

发送的任何参数都将放入类似命名的setter中。为什么不首先检查使用LiveHttpHeaders Firefox插件正确发送表单参数。

答案 1 :(得分:0)

一旦我们意识到表单数据没有正确传递到http请求,我的同事开发了一个表单数据拦截器,我们用它来手动加载数据。有关详情,请查看<interceptor><interceptor-stack><interceptor-ref>代码。