我想通过扩展OgnlValueStack
Struts 2.3.x 的类在我的应用程序中实现自定义 ValueStack。
请让我知道如何做到这一点。我需要在我的应用程序中扩展和实现哪些类以及如何使用 @Inject 注释注入不同的依赖项?
更新
我已经按照前面的建议进行了更改。我的ValueStackFactory
实现是:
package jp.co.spectrum.insight.core.mvc.factory;
import java.util.Map;
import java.util.Set;
import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
public class InsightValueStackFactory implements ValueStackFactory {
@Inject
private XWorkConverter xworkConverter;
private CompoundRootAccessor compoundRootAccessor;
@Inject("system")
private TextProvider textProvider;
@Inject
private Container container;
private boolean allowStaticMethodAccess;
/*
private static ValueStackFactory instance;
public static ValueStackFactory getInstance() {
if (instance == null) {
instance = new InsightValueStackFactory();
}
return instance;
}
*/
private static ValueStackFactory factory = new InsightValueStackFactory();
public static void setFactory(ValueStackFactory factoryParam) {
factory = factoryParam;
}
public static ValueStackFactory getFactory() {
return factory;
}
public void setXWorkConverter(XWorkConverter conv) {
this.xworkConverter = conv;
}
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
@Inject(value="allowStaticMethodAccess", required=true)
public void setAllowStaticMethodAccess(String allowStaticMethodAccess) {
this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
}
public ValueStack createValueStack() {
ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
}
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor, allowStaticMethodAccess);
container.inject(result);
stack.getContext().put(ActionContext.CONTAINER, container);
return result;
}
public void setContainer(Container container) throws ClassNotFoundException {
Set<String> names = container.getInstanceNames(PropertyAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
if (Map.class.isAssignableFrom(cls)) {
PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
}
OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
}
}
}
names = container.getInstanceNames(MethodAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
}
}
names = container.getInstanceNames(NullHandler.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
}
}
if (compoundRootAccessor == null) {
throw new IllegalStateException("Couldn't find the compound root accessor");
}
this.container = container;
}
}
该类InsightValueStackImpl
是我自定义的 ValueStack,它扩展了OgnlValueStack
.
按照前面的建议进行更改后,当我启动应用程序时,出现以下错误:
java.lang.IllegalArgumentException: Wrapped type converter cannot be null
at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.<init>(OgnlTypeConverterWrapper.java:32)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88)
at com.opensymphony.xwork2.ognl.OgnlValueStack.<init>(OgnlValueStack.java:71)
at jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.<init>(InsightValueStackImpl.java:86)
at jp.co.spectrum.insight.core.mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85)
at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.<init>(InsightFilterDispatcher.java:118)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
这是因为空 XWorkConverter 实例。
请让我知道为什么它没有被注入。
提前致谢
谢谢
我已经按照前面的建议进行了更改。我的ValueStackFactory
实现是:
package jp.co.spectrum.insight.core.mvc.factory;
import java.util.Map;
import java.util.Set;
import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
public class InsightValueStackFactory implements ValueStackFactory {
@Inject
private XWorkConverter xworkConverter ;
private CompoundRootAccessor compoundRootAccessor ;
@Inject("system")
private TextProvider textProvider ;
@Inject
private Container container;
private boolean allowStaticMethodAccess;
/*private static ValueStackFactory instance;
public static ValueStackFactory getInstance(){
if(instance==null){
instance = new InsightValueStackFactory();
}
return instance;
}*/
private static ValueStackFactory factory = new InsightValueStackFactory();
public static void setFactory(ValueStackFactory factoryParam) {
factory = factoryParam;
}
public static ValueStackFactory getFactory() {
return factory;
}
public void setXWorkConverter(XWorkConverter conv) {
this.xworkConverter = conv;
}
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
@Inject(value="allowStaticMethodAccess", required=true)
public void setAllowStaticMethodAccess(String allowStaticMethodAccess) {
this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
}
public ValueStack createValueStack() {
ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
}
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor, allowStaticMethodAccess);
container.inject(result);
stack.getContext().put(ActionContext.CONTAINER, container);
return result;
}
public void setContainer(Container container) throws ClassNotFoundException {
Set<String> names = container.getInstanceNames(PropertyAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
if (Map.class.isAssignableFrom(cls)) {
PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
}
OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
}
}
}
names = container.getInstanceNames(MethodAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
}
}
names = container.getInstanceNames(NullHandler.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
}
}
if (compoundRootAccessor == null) {
throw new IllegalStateException("Couldn't find the compound root accessor");
}
this.container = container;
}
}
该类InsightValueStackImpl
是我自定义的 ValueStack,它扩展了OgnlValueStack
.
按照前面的建议进行更改后,当我启动应用程序时,出现以下错误:
java.lang.IllegalArgumentException:在 com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88) 处的 com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.(OgnlTypeConverterWrapper.java:32) 处的包装类型转换器不能为空com.opensymphony.xwork2.ognl.OgnlValueStack.(OgnlValueStack.java:71) 在 jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.(InsightValueStackImpl.java:86) 在 jp.co.spectrum.insight.core。 mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85) 在 jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.(InsightFilterDispatcher.java:118) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)。DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 在 java.lang.Class.newInstance0(Class.java:355) 在 java.lang.Class.newInstance (Class.java:308) 在 org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) 在 org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422) 在 org.apache。 catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726 ) 在 org.apache.catalina.core.StandardHost 的 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)。在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 开始(StandardHost.java:840) .catalina.core.StandardService.start(StandardService.java:525) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 在 org.apache.catalina.startup.Catalina.start(Catalina.java :595) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang 的 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)。 reflect.Method.invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)java:840) 在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core .StandardService.start(StandardService.java:525) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 在 org.apache.catalina.startup.Catalina.start(Catalina.java:595) 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method。在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 的 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 调用(Method.java:597)java:840) 在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core .StandardService.start(StandardService.java:525) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 在 org.apache.catalina.startup.Catalina.start(Catalina.java:595) 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method。在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 的 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 调用(Method.java:597)catalina.core.ContainerBase.start(ContainerBase.java:1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.StandardService.start(StandardService.java: 525) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 在 org.apache.catalina.startup.Catalina.start(Catalina.java:595) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)catalina.core.ContainerBase.start(ContainerBase.java:1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.StandardService.start(StandardService.java: 525) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 在 org.apache.catalina.startup.Catalina.start(Catalina.java:595) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.StandardService.start(StandardService.java:525) 在 org.apache.catalina.core.StandardServer .start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl .java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start( Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)1057) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 在 org.apache.catalina.core.StandardService.start(StandardService.java:525) 在 org.apache.catalina.core.StandardServer .start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl .java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start( Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method .invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method .invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method. java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method. java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
这是因为空 XWorkConverter 实例。
请让我知道为什么它没有被注入。
提前致谢