JSON调用不使用Session Package

时间:2014-04-04 20:32:16

标签: json struts2

我有一段时间试图在实现会话的Struts2包中进行JSON调用。每当我进行调用时,都会调用相应的执行方法,但不会调用任何getter方法。因此,没有数据返回到页面。这是我的Struts.xml

<package name="agreements" extends="default" namespace="/agreements">
      ...
</package>

<package name="new-wizard" extends="agreements, json-default" namespace="/new-wizard">
    <interceptors>
        <interceptor-stack name="agreementsStack">
            <interceptor-ref name="scope">
                <param name="session">agreements, financialReportFirstReport, performanceReportFirstReport, tbvId, hasModification, tbvLoaded, subContractorGoals</param>
                <param name="key">partialAgreements</param>
            </interceptor-ref>
        <interceptor-ref name="paramsPrepareParamsStack"/>
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="agreementsStack" />
...

    <action name="subContractorGoals" class="com.mycompany.webapp.action.AgreementsSubContractorWizardAction" method="subContractorGoals">
        <result type="json">
            <param name="includeProperties">subContractorGoals.*</param>
        </result>
    </action>
</package>

这是我的javascript:

var goals = null;
$.ajax({
                async: false,
                type:'GET',
                dataType:'json',
                url:"../new-wizard/subContractorGoals",
                success:function(data) {
                    console.log("dataStringify = " + JSON.stringify(data));

                    goals = data.subContractorGoals;
                }
            });

我的控制台的输出始终相同 - {}。我找回了一个物体,但它是空的。对我来说真正令人困惑的是,如果我采取完全相同的行动并将其放在另一个包裹中,它就会起作用。

<package name="json" extends="default, json-default" namespace="/json">
    <action name="subContractorGoals" class="com.mycompany.webapp.action.AgreementsSubContractorWizardAction" method="subContractorGoals">
        <result type="json">
            <param name="includeProperties">subContractorGoals.*</param>
        </result>
    </action>
</package>

爪哇:

@SkipValidation
public String subContractorGoals()
{
    try
    {
        this.subContractorGoals = new HashMap<String, Double>();
                    this.subContractorGoals.put("test", 1.0);
        return SUCCESS;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return ERROR;
}

我之前的javascript(包名称当然更改为/json)的结果是{"subContractorGoals":{"test":"1.0"}}

我真的不知道发生了什么。

1 个答案:

答案 0 :(得分:0)

尝试更改堆栈中拦截器的顺序:

<interceptor-stack name="agreementsStack">

    <interceptor-ref name="paramsPrepareParamsStack"/>

    <interceptor-ref name="scope">
        <param name="session">agreements, financialReportFirstReport, 
                              performanceReportFirstReport, tbvId, 
                              hasModification, tbvLoaded, subContractorGoals
        </param>
        <param name="key">partialAgreements</param>
    </interceptor-ref>

</interceptor-stack>