如何在Guice Servlet中使用servlet-name进行过滤映射?

时间:2011-08-28 17:37:09

标签: java servlets guice

在我的web.xml中我有

<filter>
    <filter-name>authFilter</filter-name>
    <filter-class>com.mystuff.AuthFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>authFilter</filter-name>
    <servlet-name>requestFactoryServlet</servlet-name>
</filter-mapping>

<!-- Servlets -->
<servlet>
    <servlet-name>requestFactoryServlet</servlet-name>
    <servlet-class>com.google.web.bindery.requestfactory.server.RequestFactoryServlet</servlet-class>
    <init-param>
        <param-name>symbolMapsDirectory</param-name>
        <!-- You'll need to compile with -extras and move the symbolMaps directory 
            to this location if you want stack trace deobfuscation to work -->
        <param-value>WEB-INF/classes/symbolMaps/</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>requestFactoryServlet</servlet-name>
    <url-pattern>/gwtRequest</url-pattern>
</servlet-mapping>

我试图将其转换为使用Guice Servlet配置而不是web.xml配置。

我无法弄清楚如何在Guice中完成相同的操作。我只看到如何过滤模式的示例,而不是特定的servlet。

1 个答案:

答案 0 :(得分:6)

在web.xml中定义一个Guice监听器:

<listener>
    <listener-class>xxx.GuiceServletConfig</listener-class>
</listener>

实现这个监听器:

public class GuiceServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule() {

            @Override
            protected void configureServlets() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("symbolMapsDirectory", "WEB-INF/classes/symbolMaps/");
                serve("/gwtRequest").with(RequestFactoryServlet.class, params);


            }

        });
    }

}