【问题标题】:Struts 2 jQuery action returning entire form on successStruts 2 jQuery 动作成功返回整个表单
【发布时间】:2016-06-03 19:49:59
【问题描述】:

我有一个使用 Struts 2 jQuery 标记的 JSP。

表格是:

<s:form data-role="content" class="center-btn" id="sortForm"
            theme="simple">
<s:textfield name="item" id="item"/>
<s:textfield name="acc" id="acc"/>
</s:form>

采取行动:

@Action(value = "getLPNDetails", results = {
            @Result(name = SUCCESS, type = JSON, params = { "ignoreHierarchy", "false", "includeProperties",
                    "sortForm\\..*,actionMessages\\[\\d+\\], fieldErrors\\[\\d+\\], actionErrors\\[\\d+\\]" }),
            @Result(name = INPUT, location = "sortMUIDMobile.jsp", params = { "ignoreHierarchy", "false",
                    "includeProperties",
                    "url\\..*, actionMessages\\[\\d+\\], fieldErrors\\[\\d+\\], actionErrors\\[\\d+\\]" }),
            @Result(name = ERROR, location = "sortMUIDMobile.jsp", params = { "ignoreHierarchy", "false",
                    "includeProperties",
                    "sortForm\\..*, actionMessages\\[\\d+\\], fieldErrors\\[\\d+\\], actionErrors\\[\\d+\\]" }) })
    public String getLPNDetails() {
}

我是从哪里来的:

function lpnFilter() {
                var param = $('#sortForm').serialize();
                //var jsonValidationParam = "&struts.enableJSONValidation=true";
                //param = param + jsonValidationParam;
                $.ajax({
                    type : 'POST',
                    url : 'getLPNDetails',
                    data : param,
                    success : function(data) {
                        alert(JSON.stringify(data));
                        //alert(data.lpn);

                    },
                    async : false
                });
                //$('#lpnDetails').show();
            }

现在:

@Action(value = "getLPNDetails", results = {
                @Result(name = SUCCESS, type = JSON, params = { "ignoreHierarchy", "false", "includeProperties",
                        "sortForm\\..*,actionMessages\\[\\d+\\], fieldErrors\\[\\d+\\], actionErrors\\[\\d+\\]" }),

没有返回表单的所有值。有没有办法做到这一点?

【问题讨论】:

  • 接受答案

标签: jquery jsp struts2 struts2-jquery


【解决方案1】:

这个通过注解返回json类型结果的配置应该有参数"includeProperties", "sortForm\..*。不需要其他所有内容,因此可以将其从问题中删除。

现在您想从操作中返回 sortForm 对象。确保 action bean 位于 valuestack 的 top 上,并且包含 sortForm 的 getter 和 setter 以及其属性的 getter 和 setter。

表单应将其属性绑定到sortForm 对象。

<s:form data-role="content" class="center-btn" id="sortForm"
            theme="simple">
<s:textfield name="sortForm.item" id="item"/>
<s:textfield name="sortForm.acc" id="acc"/>
</s:form>

确保 在类路径中,并且父包是json-default 的后代。父包可以通过@ParentPackage 注解设置或者使用默认的父包xml常量。这是json 结果所必需的,以返回从操作 bean 的属性序列化的 JSON 对象。

@Result(type="json", params = {"includeProperties", "^sortForm\\..*"})  

此正则表达式匹配sortform 对象的每个属性,但您只需要两个属性。

@Result(type="json", params = {"includeProperties", "^sortForm\\.item, ^sortForm\\.acc"})  

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2016-07-10
    相关资源
    最近更新 更多