整合Guice,Servlets和Wicket

时间:2017-04-11 04:18:47

标签: java servlets wicket guice-servlet

我已经花了几天时间查看我能找到的所有例子,但我找不到一个对我有用的例子。现在大多数人似乎都是4-6岁。我觉得我只是缺少一些小东西让我的servlet和wicket应用程序与Guice一起工作。到目前为止,这个配置适用于我的wicket页面,但我的servlet无法注入任何东西。任何帮助将不胜感激。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <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://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

        <display-name>project</display-name>

        <listener>
            <listener-class>com.project.servletlistener.ServletContextListener</listener-class>
        </listener>

        <filter>
            <filter-name>guiceFilter</filter-name>
            <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>guiceFilter</filter-name>
            //Excuse the '' around the / and *. 
            //Without them it is creating a comment in stackoverflow and 
            //they are not present in my actual web.xml
            <url-pattern>'/*'</url-pattern>
        </filter-mapping>

    </web-app>


    public class ServletContextListener extends GuiceServletContextListener 
    {

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

        private ServletModule createServletModule() {
            return new ServletModule() {

                @Override
                protected void configureServlets() {

                    //One example said to include
                    //filter("/*").through(PersistFilter.class); but this resulted  
                    //in Guice configuration errors. No implementation for 
                    //com.google.inject.persist.PersistService was bound. 

                    Map<String, String> params = new HashMap<String, String>();  
                    params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
                    params.put("applicationClassName", "com.project.application.WicketApplication");

                    filter("/*").through(WicketFilter.class, params);
                    bind(WicketFilter.class).in(Singleton.class);
                    bind(WebApplication.class).to(WicketApplication.class);
                }
            };
        }
    }

1 个答案:

答案 0 :(得分:0)

这最终允许我在我的servlet中使用Guice并且仍然使用我的Wicket Code。这是一个简单的错误,结果是我对Servlets缺乏了解。所有需要做的就是用这个上下文注册servlet。

private ServletModule createServletModule() {
    return new ServletModule() {
        @Override
        protected void configureServlets() {

            Map<String, String> params = new HashMap<String, String>();  
            params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
            params.put("applicationClassName", "com.project.application.WicketApplication");

            bind(AuthorizeServlet.class);
            bind(AuthorizeCallbackServlet.class);
            bind(WicketFilter.class).in(Singleton.class);
            bind(WebApplication.class).to(WicketApplication.class);

            serve("/authorize").with(AuthorizeServlet.class);
            serve("/authorizecallback").with(AuthorizeCallbackServlet.class);
            filter("/*").through(WicketFilter.class, params);
        }

    };