如何在Wicket中使用Guice Servlet

时间:2011-03-31 01:34:07

标签: java jetty wicket guice guice-servlet

使用Guice Servlet设置我的Wicket项目后,我得到java.lang.IllegalStateException: filter path was not configured。但是,已配置过滤器路径 。我错过了什么吗?

的web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

    <display-name>node-sitter</display-name>

    <listener>
        <listener-class>com.mycompany.wicketapp.inject.ServletConfig</listener-class>
    </listener>
    <filter>
        <description>Initialises Guice</description>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Guice Servlet Listener:

public class ServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new Servlets());
    }

    private static class Servlets extends ServletModule {

        @Override
        protected void configureServlets() {
            bind(WicketFilter.class).in(Singleton.class);
            filterRegex("/.*").through(WicketFilter.class, withApplicationClass(WicketApplication.class));
        }

        private Map<String, String> withApplicationClass(Class<? extends WebApplication> applicationClass) {
            Map<String, String> initParams = new HashMap<String, String>(1);
            initParams.put("applicationClassName", applicationClass.getName());
            return initParams;
        }
    }
}

堆栈跟踪(访问页面时):

java.lang.IllegalStateException: filter path was not configured
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:124)
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:194)
    at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1323)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:474)

1 个答案:

答案 0 :(得分:10)

Apparently,可以通过向过滤器添加以下init参数来解决此问题:

initParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");