Domanda

I am trying to send data to Action in Json format through Ajax call. But i am getting following error.

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'

My attempt is following:

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();
}

StackTrace:

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)
È stato utile?

Soluzione

I think i corrected the issue.

Earlier what i was doing, setting the Action in the value stack. But Action are pushed in the stack, and this is the role of the ActionContext.

Once the Action is pushed into the ValueStack, all the property of the Action becomes ( atleast it seems to outer world) the property of ValueStack and can be accessed directly.

In my case since, Action was already pushed, so all i need to do is to set the property value of the Action.

Hence following code

vs.setValue("PersonDetailsAction", pdAction);

should be changed to

vs.setValue("firstName", pdAction.getFirstName());
vs.setValue("lastName", pdAction.getLastName());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top