Struts 2 不会将参数从 ExtJS 表单传输到 ActionSupport 类

发布于 2024-10-06 18:04:32 字数 2978 浏览 1 评论 0原文

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

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}

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

谢谢。

I am implementing a simple ExtJS form that submits to a Struts 2 ActionSupport class. The code for the various components is as follows:

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>

When the submit button is pressed, my action executes properly, and in addition to standard debugging data prints out:

null null

The JSON result is sent back correctly, but of course is also 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}

Now, it's my understanding that the values entered in the form should be inserted into the instance variables for my action class. Am I wrong in this? If not, what could be going wrong? If so, what can I do to ensure that the form data is sent to my action handler?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-10-13 18:04:32

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

Any parameters sent will be placed into the similarly named setters. Why don't you first check that the form parameters are getting sent correctly with LiveHttpHeaders Firefox plugin.

彡翼 2024-10-13 18:04:32

一旦我们意识到表单数据没有正确传递到 http 请求中,我的同事就开发了一个表单数据拦截器,我们用它来手动加载数据。有关更多信息,请查看 标签。

Once we realized the form data was not being correctly passed into the http request, my coworker developed a form data interceptor, which we use to load the data in manually. For more information, look into the <interceptor>, <interceptor-stack>, and <interceptor-ref> tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文