GWT项目在eclipse中开发完成后,就需要搬到tomcat中进行部署了
我的项目是采用 spring MVC框架的,在开发阶段(也就是在eclipse中)一切正常,本来以为直接将项目下的 war 文件下的所有文件直接copy到tomcat的ROOT中就可以的..结果出现了以下异常
严重: Exception while dispatching incoming RPC call
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletName(GenericServlet.java:322)
at javax.servlet.GenericServlet.log(GenericServlet.java:254)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.loadSerializationPolicy(RemoteServiceServlet.java:103)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.doGetSerializationPolicy(RemoteServiceServlet.java:293)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.getSerializationPolicy(RemoteServiceServlet.java:157)
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader.prepareToRead(ServerSerializationStreamReader.java:454)
at com.google.gwt.user.server.rpc.RPC.decodeRequest(RPC.java:237)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:206)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at com.xxx.xxx.spring.GWTController.handleRequest(GWTController.java:33)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
在以上异常中发现 servlet 的 ServletConfig 为Null.结合google code上的解释,这里需要改写一下google code上大部分的Spring MVC 中的 GWTController 的实现接口
原来的GWTController
public class GWTController extends RemoteServiceServlet implements Controller,
ServletContextAware {
/**
*
*/
private static final long serialVersionUID = 2275269119904991518L;
/* */
private ServletContext servletContext;
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
doPost(request, response);
return null;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
}
改写后的,实际就是 加一个接口 ServletConfigAware 并且实现其的setServletConfig(ServletConfig arg0)方法即可
public class GWTController extends RemoteServiceServlet implements Controller,
ServletConfigAware,ServletContextAware {
/**
*
*/
private static final long serialVersionUID = 2275269119904991518L;
/* */
private ServletContext servletContext;
@Override
public void setServletConfig(ServletConfig arg0) {
try {
super.init(arg0);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
doPost(request, response);
return null;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
}
最后重新编译,把项目war下的所有文件copy到ROOT下,一切正常.
其中可能会出现一些诸如以下的提示
信息: gwtrpc: ERROR: The serialization policy file '/xxxx/220A52A8DBFDA392DF4209542FE697EB.gwt.rpc' was not found; did you forget to include it in this deployment?
2011-5-5 9:35:28 org.apache.catalina.core.ApplicationContext log
信息: gwtrpc: WARNING: Failed to get the SerializationPolicy '220A52A8DBFDA392DF4209542FE697EB' for module 'http://127.0.0.1/xxxx/'; a legacy, 1.3.3 compatible, serialization policy will be used. You may experience SerializationExceptions as a result.
其实清理以下work目录即可.