💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# [已解决] 无法找到`ref-name`引用的拦截器类 > 原文: [https://howtodoinjava.com/struts2/solved-unable-to-find-interceptor-class-referenced-by-ref-name/](https://howtodoinjava.com/struts2/solved-unable-to-find-interceptor-class-referenced-by-ref-name/) 在为[**`@InterceptorRef`示例**](//howtodoinjava.com/struts-2/struts-2-custom-interceptor-with-interceptorref-example/ "Struts2 custom interceptor with @InterceptorRef example")编写代码时,我才知道此功能。 我必须在`struts.xml`文件中声明拦截器定义,而我想通过注解使用拦截器。 第一次尝试时,失败并显示以下错误: ```java Unable to load configuration. - [unknown location] at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490) Caused by: Unable to find interceptor class referenced by ref-name customStack - [unknown location] at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:63) at org.apache.struts2.convention.DefaultInterceptorMapBuilder.buildInterceptorList(DefaultInterceptorMapBuilder.java:95) at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:86) at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:64) ``` ## 原因: 默认情况下,`Convention`插件使用其自己的包`Convention-default`,其中不包含您在`struts.xml`中定义的包。 这意味着`Convention`将在其中放置您的操作的包不会扩展定义拦截器的包。 ![Random exceptions](https://img.kancloud.cn/81/c9/81c97e6ebb55a2bdb88e52750e274fe3_300x350.png) 随机异常 ## 解决方案 要更改,您有两个选择: 1) 使用`@ParentPackage`注解 2) 在`struts.xml`中定义`<constant name=”struts.convention.default.parent.package” value=”default”/ >` 例如: ```java <struts> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="demoCustomInterceptor" class="com.howtodoinjava.struts2.example.web.DemoCustomInterceptor" /> <interceptor-stack name="customStack"> <interceptor-ref name="demoCustomInterceptor"/> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> </package> <constant name="struts.convention.result.path" value="/WEB-INF/jsp/" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action," /> <constant name="struts.custom.i18n.resources" value="test" /> <constant name="struts.convention.default.parent.package" value="default"/> </struts> ``` 这样可以解决问题。 **祝您学习愉快!**