我在我的测试网络项目中尝试Guice 3.0。我在web.xml中注册了GuiceFilter和GuiceServletContextListener。比我在ServletModule中创建并注册了一个servlet:
public class MyModule extends AbstractModule {
@Override
protected void configure() {
install(new ServletModule() {
@Override
protected void configureServlets() {
serve("/enter").with(Dispatcher.class);
}
});
bind(Logger.class).toInstance(Logger.getLogger("myLogger"));
}
}
按预期调用Dispather,但是当它转发到JSP页面时,我收到错误:
request.getRequestDispatcher(“/ persons.jsp”)。forward(request,response);
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [56] in the generated java file: [C:\Users\user\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\web1\org\apache\jsp\persons_jsp.java]
The method getDispatcherType() is undefined for the type HttpServletRequest
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
servlets.Dispatcher.showPersons(Dispatcher.java:81)
servlets.Dispatcher.process(Dispatcher.java:50)
servlets.Dispatcher.doGet(Dispatcher.java:86)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
我认为这是由于Tomcat 8.0使用的servlet-api 3.1和guice使用的servlet-api 2.5无法实现的。
在没有将我的应用服务器降级到servlet-api 2.5的情况下,是否有解决此问题的方法?
Tomcat的文档说使用2.5的最新版本是Tomcat 6。
更新 事实证明,servlet-api在guice-servlet中被声明为“提供”依赖。我所要做的就是正确地声明我的依赖项以省略这个lib。我使用常春藤的当前配置:
<ivy-module version="2.0">
<info organisation="myorg" module="mymodule"/>
<dependencies defaultconfmapping="*->default">
<dependency org="com.google.inject" name="guice" rev="3.0"/>
<dependency org="org.hibernate" name="hibernate-core" rev="4.3.8.Final"/>
<dependency org="com.google.inject.extensions" name="guice-servlet" rev="3.0"/>
</dependencies>
</ivy-module>