java-如何使用注释链接自定义拦截器

我手头有一个自定义拦截器,我希望它可以通过注释与Action类关联.我已经使用struts.xml中声明的那个添加了拦截器引用,但是由于

 "SEVERE: Exception starting filter Struts2
Unable to load configuration. - [unknown location]" 

我的Action类别如下,

@Action(value = "/login", results = {
            @Result(name = "success", location = "/Welcome.jsp"),
            @Result(name = "error", location = "/Login.jsp") },
            interceptorRefs = {
                @InterceptorRef("defaultStack"),
                 @InterceptorRef("myLogging")}
    )
    public String validateCredentials() {
        if (this.userName.equals("Raj") && this.password.equals("aspire")) {
            return "success";
        } else {
            // addActionError(getText("error.login"));
            return "error";
        }
    }

我的struts.xml如下,

<package name="default" extends="struts-default" namespace="/">
        <interceptors>
            <interceptor name="myLogging" class="org.struts.hello.MyLoggingInterceptor"/>
        </interceptors>
    </package>

错误堆栈:

SEVERE: Exception starting filter Struts2
Unable to load configuration. - [unknown location]
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)
    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:3838)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4488)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:519)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: Unable to find interceptor class referenced by ref-name loggingStack - [unknown location]
    at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:52)
    at org.apache.struts2.convention.DefaultInterceptorMapBuilder.buildInterceptorList(DefaultInterceptorMapBuilder.java:99)
    at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:90)
    at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:74)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:676)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:493)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:278)
    at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:52)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:200)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
    ... 21 more
Dec 22, 2011 3:30:22 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Dec 22, 2011 3:30:22 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/StrutsAnnotation] startup failed due to previous errors

解决方法:

我在使用带有或不带有struts2的注释时没有太多经验,但是我相信您使用的是convention-plugin.以下是他们对您的问题的简要介绍.

If you get errors like “Unable to find interceptor class referenced by
ref-name XYZ”. This means that the package where Convention is placing
your actions, does not extend the package where the interceptor is
defined. To fix this problem either

  1. Use @ParentPackage annotation(or struts.convention.default.parent.package)
    passing the name of the package that defines the interceptor.
  2. Create a package in XML that extends the package that defines the interceptor,
    and use @ParentPackage(or Struts.convention.default.parent.package) to point to it.

有关更多详细信息,请参阅该文档

convention-plugin

上一篇:mybatis源码配置文件解析之四:解析plugins标签


下一篇:servlet容器中listener、filter、interceptor的启动顺序