Hooking Spring 2.5.6 and GWT 1.6 - getServletContext returns N

141 views
Skip to first unread message

colt54

unread,
Apr 21, 2009, 12:51:11 PM4/21/09
to Google Web Toolkit
Hi,

I am trying to use a GWT service with Spring.
I am using DispatcherServlet from Spring, GWTHandler and my service is
defined as a bean in a mywebapp-servlet.xml file.
I have also beans intialized at startup with ContextLoadListener.
My service works fine except if I try to call getServletContext from
it. In that case I have a NullPointerException. As a consequence I can
not make reference o my other beans from my service.

java.lang.NullPointerException: null
at
com.google.gwt.user.server.rpc.RPCServletUtils.writeResponseForUnexpectedFailure
(RPCServletUtils.java:248)
at
com.google.gwt.user.server.rpc.RemoteServiceServlet.doUnexpectedFailure
(RemoteServiceServlet.java:285)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost
(RemoteServiceServlet.java:99)
at com.mycompany.admin.admintool.server.GWTController.handleRequest
(GWTController.java:35)
at
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle

Details of my web.xml

<servlet>
<servlet-name>mywebapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</
servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mywebapp</servlet-name>
<url-pattern>/geofamilyadmingwt/mywebapp.rpc</url-pattern>
</servlet-mapping>

I have some other beans that are initialized with
ContextLoaderListener :

<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/main-context.xml /WEB-INF/dao-context.xml /WEB-
INF/business-context.xml /WEB-INF/drivers-context.xml /WEB-INF/map-
context.xml</param-value>
</context-param>


Code of my GWTController :


public class GWTController extends RemoteServiceServlet implements
Controller {
// Instance fields
private RemoteService remoteService;
private Class remoteServiceClass;
private ServletContext servletContext;


// Public methods
/**
* Call GWT's RemoteService doPost() method and return null.
*
* @param request The current HTTP request
* @param response The current HTTP response
* @return A ModelAndView to render, or null if handled directly
* @throws Exception In case of errors
*/
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
super.doPost(request, response);
return null; // response handled by GWT RPC over XmlHttpRequest
}

/**
* Process the RPC request encoded into the payload string and
return a
* string that encodes either the method return or an exception
thrown by
* it.
* @param payload The RPC payload
*/
public String processCall(String payload) throws
SerializationException {
try {
RPCRequest rpcRequest = RPC.decodeRequest(payload,
this.remoteServiceClass);

// delegate work to the spring injected service
return RPC.invokeAndEncodeResponse(this.remoteService,
rpcRequest
.getMethod(), rpcRequest.getParameters());
} catch (IncompatibleRemoteServiceException e) {
getServletContext()
.log(
"An IncompatibleRemoteServiceException was thrown
while processing this call.",
e);

return RPC.encodeResponseForFailure(null, e);
}
}

@Override
public ServletContext getServletContext() {
return servletContext;
}

public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}


/**
* Setter for Spring injection of the GWT RemoteService object.
*
* @param RemoteService
* The GWT RemoteService implementation that will be
delegated to
* by the {@code GWTController}.
*/
public void setRemoteService(RemoteService remoteService) {
this.remoteService = remoteService;
this.remoteServiceClass = this.remoteService.getClass();
}
}

Code of my service implementation:

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet
implements
GreetingService {

private transient ApplicationContext ac;
private ClientBusiness clientBusiness;

public String greetServer(String clientLogin) {
String result = "Hello";
boolean res;
String serverInfo = this.getServletContext().getServerInfo(); <--
throws the NullPointerException
return result;
}

I tried to override getServletContext method with the code below but
no success.
@Override
public ServletContext getServletContext() {

return getThreadLocalRequest().getSession().getServletContext
();

}

How can I solve this problem ?

Thanks,
C.

Marcin Zawadzki

unread,
Apr 24, 2009, 1:58:38 AM4/24/09
to Google-We...@googlegroups.com
Hi,

Try following approach - works for me. Rather then extends your service implementation with RemoteServiceServlet make sure your service is context aware - implement ServletContextAware. So you'll have:

@SuppressWarnings("serial")
public class GreetingServiceImpl implements GreetingService,ServletContextAware {


   private transient ApplicationContext ac;
       private ClientBusiness clientBusiness;
       private ServletContext servletContext;


       public String greetServer(String clientLogin) {
               String result = "Hello";
               boolean res;
               String serverInfo = servletContext.getServerInfo();   <--  injected by spring from your  mywebapp-servlet.xml file    
           return result;
       }

     public void setServletContext(ServletContext arg0) {
        // TODO Auto-generated method stub
        this.servletContext = arg0;
     }
}

Thanks,
Marcin
--
Greetings,
Marcin
Reply all
Reply to author
Forward
0 new messages