读书人

struts2 ognl 错误

发布时间: 2012-06-26 10:04:14 作者: rapoo

struts2 ognl 异常
ERROR [com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler] - Could not create and/or set value back on to object

at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1285)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:77)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.Ognl.setValue(Ognl.java:737)



另一种解决方式,自己捕获异常,并在异常处理代码中添加filderror:

if (context != null && (Boolean.TRUE.equals(context.get(XWorkConverter.REPORT_CONVERSION_ERRORS)))) {
String realProperty = "loc"; //这要转换属性名称
String fullName = (String) context.get(XWorkConverter.CONVERSION_PROPERTY_FULLNAME);

if (fullName != null) {
realProperty = fullName;
}

Map<String, Object> conversionErrors = (Map<String, Object>) context.get(ActionContext.CONVERSION_ERRORS);

if (conversionErrors == null) {
conversionErrors = new HashMap<String, Object>();
context.put(ActionContext.CONVERSION_ERRORS, conversionErrors);
}
conversionErrors.put(realProperty, value);
}
return null;



引用
struts2.x 严重: Could not create and/or set value back on to object
2010-03-17 09:02
在用struts2.1提交表单时发生以下错误:

2010-3-17 8:51:08 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
严重: Could not create and/or set value back on to object
java.lang.InstantiationException: com.model.GuestInfo
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)

....
原因:经过分析,是因为在表单提交的时候,action并没有创建表单对应的实例。

因为用人在写表单对应的类时,写了一个带有参数的构造方法,而在action中只是定义了这个类,没有实现。

解决方法:方法1、在表单对应的类中,再建个什么也不做的构造方法,即不带有参数和方法的构造方法。

方法 2、在action中,new出表单对应的类。

本人用第一种方法。




读书人网 >软件架构设计

热点推荐