2017-04-11 53 views
0

我花了幾天的時間查看所有可以找到的例子,而且我還沒有找到一個能爲我工作的例子。現在大多數人似乎已經4-6歲了。我覺得我只是缺少一些小東西來讓我的servlets和wicket應用程序與Guice一起工作。到目前爲止,該配置與我的檢票頁面一起工作,但我的servlet無法注入任何內容。任何幫助,將不勝感激。集成Guice,Servlets和Wicket

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

回答

0

這就是最終讓我在我的Servlet中使用Guice和仍然有我的檢票碼的工作。這是一個簡單的錯誤,原來我對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); 
     } 

    };