0

次のように、インターセプターとインターセプター スタックを定義します。

<package name="interceptor" extends="struts-default">
    <interceptors>
        <interceptor name="log"
            class="com.log.LogInterceptor" />
        <interceptor-stack name="interceptorStack">
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="log" />
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="interceptorStack" />
</package>

LogInterceptor クラスは次のようになります。

package com.log;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LogInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;
private Properties prop;

@Override
public void init() {
    InputStream in = Object.class.getResourceAsStream( "/com/log/interceptor.properties" );
    try {
        prop.load( in );
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println( "init" );
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    String actionName = invocation.getInvocationContext().getName();
    System.out.println( actionName );
    long startTime = System.currentTimeMillis();
    invocation.invoke();
    long endTime = System.currentTimeMillis();
    System.out.println( endTime - startTime );
    return null;
}
}

init() でプロパティ ファイルを読み取りたいのですが、Tomcat を起動すると、次のようになります。

Caught Exception while registering Interceptor class com.log.LogInterceptor - interceptor - file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/EnergySystem/WEB-INF/classes/struts.xml:28:50
at org.apache.struts2.impl.StrutsObjectFactory.buildInterceptor(StrutsObjectFactory.java:77)
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:70)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:1043)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStack(XmlConfigurationProvider.java:860)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStacks(XmlConfigurationProvider.java:873)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptors(XmlConfigurationProvider.java:896)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:476)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
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.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
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)
Caused by: java.lang.NullPointerException
at com.log.LogInterceptor.init(LogInterceptor.java:29)
at org.apache.struts2.impl.StrutsObjectFactory.buildInterceptor(StrutsObjectFactory.java:57)
... 41 more

このコードを他のクラスに入れてmain()で呼び出すと、ファイルを正常に読み取ることができるため、プロパティを読み取るコードは正しく見えます.init()で失敗するのはなぜでしょうか.助けてください,ありがとう!

解決策:私はインスタンス化されたプロップにあまりにも不注意でしProperties prop = new Properties()LogInterceptor.class.getResourceAsStream

4

1 に答える 1

1

Objectクラスの代わりにクラスを使用して、リソースをロードしてみてください。

LogInterceptor.class.getResourceAsStream("interceptor.properties");
于 2013-03-13T10:36:27.763 に答える