【问题标题】:Integrating Guice, Servlets And Wicket集成 Guice、Servlet 和 Wicket
【发布时间】:2017-04-11 04:18:46
【问题描述】:

我已经花了几天时间浏览了所有我能找到的示例,但我还没有找到适合我的示例。现在大多数似乎是4-6岁。我觉得我只是缺少一些小东西来让我的 servlet 和 wicket 应用程序与 Guice 一起工作。到目前为止,此配置适用于我的检票口页面,但我的 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);
                }
            };
        }
    }

【问题讨论】:

    标签: java servlets wicket guice-servlet


    【解决方案1】:

    这就是最终允许我在我的 servlet 中使用 Guice 并且我的 Wicket Code 仍然可以工作的原因。这是一个简单的错误,原来是我对 Servlet 缺乏了解。所需要做的就是使用此上下文注册 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);
            }
    
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 2013-11-07
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2013-05-02
      相关资源
      最近更新 更多