我正尝试通过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)
答案 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());