1

OgnlValueStackStruts 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 インスタンスが null であるためです。

注入されない理由を教えてください。

前もって感謝します

ありがとう


以前に提案されたように変更を加えました。私の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.OgnlTypeConverterWrapper.(OgnlTypeConverterWrapper.java:32) でラップされた型コンバーターを null にすることはできません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) at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.(InsightFilterDispatcher.java:118) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(ネイティブ メソッド) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) で、sun.reflect で。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.ContainerBase.start(ContainerBase.java:1057) で 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) で 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) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at 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) 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) で、sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) で、sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) で、java.lang.reflect.Method で。 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) で invoke(Method.java:597) 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) 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) で、sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) で、sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) で、java.lang.reflect.Method で。 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) で invoke(Method.java:597) org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) で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.invoke(Method.java:597) でorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)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) 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.invoke(Method.java:597) でorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at 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) で org.apache.catalina.startup.Catalina.start(Catalina.java:595) で sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブ メソッド) で 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) で org.apache.catalina.startup.Catalina.start(Catalina.java:595) で sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブ メソッド) で 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 (ネイティブ メソッド) で.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at 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 (ネイティブ メソッド) で.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at 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 インスタンスが null であるためです。

注入されない理由を教えてください。

前もって感謝します

4

1 に答える 1

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 メソッドに戻してみて、問題が解決するかどうかを確認してください。

于 2013-09-04T03:21:31.807 に答える