我花了几天时间浏览所有我能找到的例子,但我还没有找到一个对我有用的例子。现在大多数似乎是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);
}
};
}
}