简体   繁体   中英

How to use Guice Servlet with Wicket

After setting up my Wicket project with Guice Servlet, I get a java.lang.IllegalStateException: filter path was not configured . The filter path is configured, though. Am I missing something?

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;
        }
    }
}

Stack Trace (When the page is visited):

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM