该Bug的异常栈如下:
Unable to set parameter [id] in result of type [org.apache.struts2.dispatcher.ServletActionRedirectResult] ==> com.opensymphony.xwork2.util.logging.commons.CommonsLogger.error(CommonsLogger.java:27)
Caught OgnlException while setting property 'id' on type 'org.apache.struts2.dispatcher.ServletActionRedirectResult'.
- Class: ognl.ObjectPropertyAccessor
File: ObjectPropertyAccessor.java
Method: setProperty
Line: 132 - ognl/ObjectPropertyAccessor.java:132:-1
at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:392)
at com.opensymphony.xwork2.ognl.OgnlUtil.setProperty(OgnlUtil.java:143)
at com.opensymphony.xwork2.ognl.OgnlReflectionProvider.setProperty(OgnlReflectionProvider.java:91)
at com.opensymphony.xwork2.ObjectFactory.buildResult(ObjectFactory.java:221)
at com.opensymphony.xwork2.DefaultActionInvocation.createResult(DefaultActionInvocation.java:208)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:355)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
....
....
....
Caused by: ognl.NoSuchPropertyException: org.apache.struts2.dispatcher.ServletActionRedirectResult.id
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:132)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:385)
如果我们在redirectAction result中使用动态参数就会出现如上异常,如下:
<result name="articleDetail" type="redirectAction">
<param name="actionName">article</param>
<param name="method">detail</param>
<param name="parse">true</param>
<param name="id">${id}</param>
</result>
这个bug在struts2.1.6中尚没有解决,bug出现的地方位于com.opensymphony.xwork2.ognl.OgnlUtil
try {
setValue(name, context, o, value);
} catch (OgnlException e) {
Throwable reason = e.getReason();
String msg = "Caught OgnlException while setting property '" + name + "' on type '" + o.getClass().getName() + "'.";
Throwable exception = (reason == null) ? e : reason;
if (throwPropertyExceptions) {
throw new ReflectionException(msg, exception);
} else {
if (devMode) {
LOG.warn(msg, exception);
}
}
}
原因是Ognl试图在ServletActionResult中寻找一个叫id的属性,找不到就抛出了这个Ognl异常。但是ServletActionResult最终还是能拿到这个动态参数,并且使用它来构造查询串。这个对程序的执行结果没有影响,但服务器端总是打出这样的log,让人感觉很不舒服。或者我们可以选择改变struts2的log level来避免它!