GWTSpringController, #2

9 views
Skip to first unread message

georgeuoa

unread,
Jul 13, 2006, 8:54:18 AM7/13/06
to Google Web Toolkit
Dear All

I'll try to summarise in this thread the usage of the
GWTSpringController [1], which is most useful for integrating GWT with
a Spring web application. By extending this controller (and
implementing your remote service interface) you can call GWT services
from your client application and have them managed by Spring. This
allows you to use Spring's excellent bean injection, URL mapping,
APO, transaction management, DAO binding etc. The controller is rather
simple and all it does is joining the required Controller interface
with the GWT servlet into an abstract class which serves as a fundament
for your own GWT services.

I'll give you a raw description of how this glues together, spring
folks won't find anything surprising here.

1. First you need your regular service interface (and the asynchronous
version!), such as

package myservice.math;

public interface MathService extends RemoteService {

String sum(int a, int b);
}

2. Then you implement your service extending the GWTSpringController:

public class MathServiceImpl extends GWTSpringController implements
MathService {

public String sum(int a, int b){
return ""+a+" + "+b+" = " + (a+b);
}

}
3. Remove the GWT servlet from web.xml, since that functionality is now
taken over by Spring. The only servlet in this regard needed is the
Spring servlet, so web.xml becomes something like:

<servlet>
<servlet-name>math</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>math</servlet-name>
<url-pattern>*.math</url-pattern>
</servlet-mapping>


4. Register the controller in math-servlet.xml (for each servlet
created with spring, it looks up by default a file called
servletname-servlet.xml, so here that is math-servlet.xml, since the
servlet is called math).

<beans>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">
<props>
<prop key="*.math">MathController</prop>
</props>
</property>

</bean>

<bean id="MathController"
class=" myservice.math,MathServiceImpl"/>

</beans>

5. Don't forget to point the service URL from inside your
entrypoint/widget to the right location, which in the example I gave
matches any url ending with .math:
MathAsync math = (MathAsync) GWT.create(MathService.class);
ServiceDefTarget target = (ServiceDefTarget) queryService;
target.setServiceEntryPoint("add.math");


I hope that cleared things up a little.
G.

[1]

package springcontroller;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
* Implementation of a GWT-Spring controller that forwards a request
made
* to the doPost method.
* @author Georgovassilis
*/

public abstract class GWTSpringController extends RemoteServiceServlet
implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
doPost(request, response);
return null;
}

}

semiot...@gmail.com

unread,
Jul 13, 2006, 3:51:44 PM7/13/06
to Google Web Toolkit
Yes, it does clear things up for me. I now see the point: being able
to call the GWT services from other parts of your Spring application
(on the server side). Yes, that is good.

richard...@gmail.com

unread,
Jul 14, 2006, 2:00:55 AM7/14/06
to Google Web Toolkit
In step 5, would that be
target.setServiceEntryPoint("add.math");

or
target.setServiceEntryPoint("sum.math");

Forgive my Spring ignorance, I'm just wondering how it knows what
function to call.

georgeuoa

unread,
Jul 14, 2006, 2:48:50 AM7/14/06
to Google Web Toolkit
Hi Richard

It could be anything, as long as it ends in .math! Remember the servlet
mapping in web.xml? It says

<servlet-mapping>
<servlet-name>math</servlet-name>
<url-pattern>*.math</url-pattern>
</servlet-mapping>

so it'll passes anything that ends in .math to the Spring servlet. And
then in math-servlet.xml :

<property name="mappings">
<props>
<prop key="*.math">MathController</prop>
</props>
</property>

which maps again the same url to MathController. Of course, if you
wanted, you can map urls then to different controllers like:

<property name="mappings">
<props>
<prop key="add.math">MathAddingController</prop>
<prop key="sub.math">MathSubstractingController</prop>
</props>

Jean-Francois Briere

unread,
Jul 14, 2006, 3:01:48 AM7/14/06
to Google Web Toolkit
As long as the entry point ends with .math then you can put whatever
you want before.

You could do:
target.setServiceEntryPoint("toto.math");
and it would work.

The entry point is useful only to activate the right servlet (the
DispatcherServlet servlet from step -3-)
It's also useful to forward the request to the right implemented custom
controller (which is set in step -4-)
But it is NOT used to set the method that will be remotely called.
This is done by actually making the async call (not shown previously).

Here you should have the async interface:

package myservice.math;

public interface MathServiceAsync {
public void sum(int a, int b, AsyncCallback callback);
}

Then do:

MathServiceAsync math = (MathServiceAsync)
GWT.create(MathService.class);
ServiceDefTarget target = (ServiceDefTarget) math ;
target.setServiceEntryPoint("toto.math"); // <<-- anything.math

And finally make the actual remote call:

math.sum(2, 2, new AsyncCallback() {
public void onSuccess(Object result) {
...
}
public void onFailure(Throwable caught) {
...
}
});

Regards

Reply all
Reply to author
Forward
0 new messages