我尝试创建使用Jetty作为servlet容器的Spring MVC应用程序。我的方法的特点是通过bean在Spring中运行Jetty:
<bean id="server" class="org.mortbay.jetty.Server" destroy-method="stop">
<property name="connectors">
<list>
<bean id="connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="port" value="9080" />
</bean>
</list>
</property>
<property name="handlers">
<list>
<ref bean="basicContext" />
</list>
</property>
</bean>
<bean name="basicContext" class="org.mortbay.jetty.servlet.Context">
<property name="contextPath" value="/" />
<property name="servletHandler">
<bean class="org.mortbay.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean class="org.mortbay.jetty.servlet.ServletHolder">
<property name="name" value="spring-mvc" />
<property name="servlet">
<bean class="name.krestjaninoff.hello.util.DispatcherServletWrapper" />
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean class="org.mortbay.jetty.servlet.ServletMapping">
<property name="servletName" value="spring-mvc" />
<property name="pathSpec" value="/*" />
</bean>
</list>
</property>
</bean>
</property>
</bean>
要将应用程序上下文传递给我的servlet,我使用实现ContextAware接口的DispatcherServlet的简单包装器:
public class DispatcherServletWrapper extends DispatcherServlet implements
ApplicationContextAware {
private static final long serialVersionUID = -5870408422415688046L;
private ApplicationContext appContext;
private ServletContext servletContext;
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
this.appContext = arg0;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
protected WebApplicationContext createWebApplicationContext(
WebApplicationContext arg0) throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.setParent(appContext);
wac.refresh();
return wac;
}
}
应用程序启动良好,但当我尝试获取某个页面时,我会得到下一个异常:
2010-12-08 09:50:22,684 [29071401@qtp-4084687-0] DEBUG name.krestjaninoff.hello.util.DispatcherServletWrapper
- org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:671)
- Could not complete request
java.lang.NullPointerException
at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:124)
at org.springframework.web.servlet.view.JstlView.exposeHelpers(JstlView.java:135)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:211)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
因此,当JstlView类尝试获取ServletContext时会出现问题。 JstlView实现了ServletContextAware接口,因此他必须了解ServletContext。
在我看来,问题在于没有创建ServletContext实例。有人能帮助我吗?