我正在使用maven,我将urlrewrite.xml放在源文件夹src / main / resources /下。像这样配置web.xml:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>classpath:urlrewrite.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
但是得到错误:org.tuckey.web.filters.urlrewrite.UrlRewriteFilter错误:无法在classpath找到urlrewrite conf文件:urlrewrite.xml。如果我把它放在默认的loc:/WEB-INF/urlrewrite.xml中。有用。但我想符合maven规范并将所有配置放在资源文件夹下。
有人可以帮助我吗?感谢。
答案 0 :(得分:2)
使用此
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
并将urlrewrite.xml文件放在'WEB-INF'中 This article有设置说明。
答案 1 :(得分:0)
这不是世界上最干净的解决方案,但我找到了解决办法。请注意,我的应用程序是基于弹簧的:
公共类EnhancedUrlRewriteFilter扩展了UrlRewriteFilter {
private String confPath;
private final Log logger = LogFactory.getLog (getClass ());
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String confPathStr = filterConfig.getInitParameter("confPath");
confPath = StringUtils.trim(confPathStr);
super.init (filterConfig);
}
@Override
protected void loadUrlRewriter (FilterConfig aFilterConfig) throws ServletException {
try {
configure (aFilterConfig.getServletContext ());
}
catch (Throwable e) {
throw new IllegalArgumentException (e);
}
}
private void configure (ServletContext context) throws IOException {
ResourceLoader webApplicationContext = WebApplicationContextUtils.getWebApplicationContext (context);
Resource resource = webApplicationContext.getResource (confPath);
logger.debug ("Loaded urlrewrite.xml from " + resource.getFilename ());
InputStream inputStream = resource.getInputStream();
URL confUrl = null;
try {
confUrl = context.getResource(confPath);
} catch (MalformedURLException e) {
logger.debug(e);
}
String confUrlStr = null;
if (confUrl != null) {
confUrlStr = confUrl.toString();
}
if (inputStream != null) {
Conf conf = new Conf(context, inputStream, confPath, confUrlStr, false);
checkConf(conf);
}
}
}
并在我的web.xml中:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>EnhancedUrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>classpath:urlrewrite.xml</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>