Struts 2中的自定义OgnlValueStack

时间:2013-09-02 15:12:22

标签: struts2 valuestack

我希望通过扩展Struts 2.3.x的OgnlValueStack类在我的应用程序中实现自定义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:Wrapped类型转换器不能为null     在com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper。(OgnlTypeConverterWrapper.java:32)     在com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88)     在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)     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)     在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)     在org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)     在org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)     在org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)     在org.apache.catalina.core.StandardHost.start(StandardHost.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)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     at 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实例为空。

请让我知道为什么没有注射。

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要实现ValueStackFactory并在struts.xml中注册它,如下所示:

<bean type="com.opensymphony.xwork2.util.ValueStackFactory"
      name="yourOgnlValueStackFactory"
      class="com.example.YourOgnlValueStackFactory" />

然后,使用以下命令将您的实现设置为要使用的工厂:

<constant name="struts.valueStackFactory" value="yourOgnlValueStackFactory"/>

<强>更新

我不确定你是否能像你一样混合场和方法注入。尝试将@Inject注释移回setter方法,看看是否能解决问题。