2012-02-29 52 views
1

我正在使用maven,並將urlrewrite.xml放在源文件夾src/main/resources /下。配置的web.xml是這樣的:UrlRewriteFilter錯誤

<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規範並將所有配置放在資源文件夾下。

任何人都可以幫助我嗎?謝謝。

回答

2

使用 'WEB-INF' This article這裏面

<init-param> 
    <param-name>confPath</param-name> 
    <param-value>/WEB-INF/urlrewrite.xml</param-value> 
</init-param> 

,並把urlrewrite.xml文件有安裝說明。

+0

我可以把它放在其他地點?那麼confPath param有什麼用? – 2012-02-29 09:22:07

+0

我發現這個問題已經修復http://code.google.com/p/urlrewritefilter/issues/detail?id=28據他們說,首先過濾器會嘗試從servlet上下文中加載它。如果它 找不到一個,它會嘗試從系統的類加載器加載它。 – tusar 2012-02-29 09:51:25

+0

我明白了。我試圖通過實現我自己的UrlRewriteFilter來解決這個問題,但它似乎有太多的依賴關係。所以我放棄了。我將不得不使用上下文路徑。非常感謝。 – 2012-02-29 10:09:44

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> 
相關問題