CompoundRoot中没有对象具有名为的可公开访问的属性

时间:2014-01-30 09:48:38

标签: java struts interceptor

我正尝试通过Action调用以Json格式向Ajax发送数据。但我得到以下错误。

1.  No object in the CompoundRoot has a publicly accessible property named 'PersonDetailsAction' (no setter could be found).
2.  Error setting expression 'PersonDetailsAction' with value 'actions.PersonDetailsAction@680e6039'

我的尝试是:

struts.xml中

<package name="simple" extends="struts-default" namespace="/">

    <interceptors>

        <interceptor name="jsonToPOJO" class="interceptors.FromJsonInterceptor" />

        <interceptor-stack name="jsonToPOJOStack">
            <interceptor-ref name="jsonToPOJO"></interceptor-ref>
        </interceptor-stack>
    </interceptors>
    <action name="PersonDetailAction" class="actions.PersonDetailsAction"
        method="execute">
        <interceptor-ref name="jsonToPOJOStack"></interceptor-ref>
        <result name="success" type="dispatcher">/welcome.html</result>
    </action>
</package>

home.html的

function send() {
    var jsonData = {
        "firstName" : "John",
        "lastName" : "Smith",
    }

    $.ajax({
        url : './PersonDetailAction.action',
        type : 'post',
        data: JSON.stringify(jsonData),
        contentType: "application/json; charset=utf-8",
        traditional: true,
        success : function(data) {
            document.getElementById("result").innerHTML = data;
        },
    });
}

PersonDetailsAction.java

public class PersonDetailsAction {
    String firstName, lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String execute(){
        return "success";
    }
}

FromJsonInterceptor

public String intercept(ActionInvocation actInvc) throws Exception {
    ServletRequest request = ServletActionContext.getRequest();
    Gson gsonObject = new Gson();
    PersonDetailsAction pdAction;

    pdAction = gsonObject.fromJson(request.getReader(), PersonDetailsAction.class);
    System.out.println(pdAction.getFirstName());
    System.out.println(pdAction.getLastName());
    ValueStack vs = actInvc.getStack();
    vs.setValue("PersonDetailsAction", pdAction);
    return actInvc.invoke();
}

堆栈跟踪:

No object in the CompoundRoot has a publicly accessible property named 'PersonDetailsAction' (no setter could be found). - [unknown location]
com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:106)
ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2332)
ognl.ASTProperty.setValueBody(ASTProperty.java:127)
ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
ognl.SimpleNode.setValue(SimpleNode.java:301)
ognl.Ognl.setValue(Ognl.java:737)
com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:234)
com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:183)
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:170)
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:164)
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:157)
interceptors.FromJsonInterceptor.intercept(FromJsonInterceptor.java:38)

1 个答案:

答案 0 :(得分:0)

我想我已经纠正了这个问题。

早些时候我在做什么,在值堆栈中设置Action。但Action被推入堆栈,这是ActionContext的作用。

Action推入ValueStack后,Action的所有属性变为(至少在外部世界)ValueStack的属性,并且可以直接访问。

在我的情况下,Action已被推送,所以我需要做的就是set the property value of the Action

因此遵循代码

vs.setValue("PersonDetailsAction", pdAction);

应改为

vs.setValue("firstName", pdAction.getFirstName());
vs.setValue("lastName", pdAction.getLastName());