如何使用Annotation链接自定义拦截器

时间:2011-12-22 06:24:56

标签: java struts2 interceptor

我手头有一个自定义拦截器,我想让它与一个使用注释的Action类相关联。我使用struts.xml中声明的那个添加了interceptor-ref,但是得到了异常,

 "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

1 个答案:

答案 0 :(得分:3)

我没有使用带有或不带struts2的注释的exp,但我相信你正在使用convention-plugin。这里是他们对你的问题说的快速镜头。

  

如果您收到“无法找到引用的拦截器类”之类的错误   ref-name XYZ“。这意味着公约所在的包裹   你的行为,不会扩展拦截器所在的包   定义。要解决这个问题

     
      
  1. 使用@ParentPackage注释(或struts.convention.default.parent.package)   传递定义拦截器的包的名称。
  2.   
  3. 用XML创建一个包,用于扩展定义拦截器的包,   并使用@ParentPackage(或Struts.convention.default.parent.package)指向它。
  4.   

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

convention-plugin