GWT J2EE Spring integration

203 views
Skip to first unread message

ukid...@mac.com

unread,
Jan 21, 2007, 11:06:57 PM1/21/07
to Google Web Toolkit
GWT (Google Web Toolkit) is probably the biggest breakthrough for
developers since Spring and Hibernate.

Now, I recommend GWT/Spring/Hibernate.

Creating GWT JavaScript using Java is relatively easy and with few
hours of practice you can become proficient in even complex tasks such
as hierarchical tree representations (client-server), all of that
without writing any JavaScript!

The problem I encountered was with integration of my newly created
widgets with existing Spring/Struts/Hibernate application.
Because of scarce existing documentation, I decided to write a short
note on the subject.

----

Dependencies:
For this project I added the following jars to the existing J2EE
project:

- gwt-servlet.jar
- asm-1.5.3.jar
- gwt-widgets-server-0.1.2.jar
- javassist-3.0.jar
- cglib-2.1_3.jar


I recommend to manage jar using Maven2.

----

I started with creating a separate GWT project which is relatively easy
to follow from the Google Website.
After the client Java got compiled into JavaScript I liked I copied the
content of the www directory to my J2EE project.
I put all the files into my Web Root directory:
workspace/MyEclipseProject/WebContent/gwt/com.XYZ.WorkflowTree/WorkflowTree.html

----

I included the reference to my JavaScript code in one of the JSP pages:

<!-- OPTIONAL: include this if you want history support -->
<iframe id="__gwt_historyFrame"
style="width:0;height:0;border:0"></iframe>
<script language="javascript"
src="gwt/com.XYZ.WorkflowTree/gwt.js"></script>
<div id="slot1"/>


Notice that slot1 is a div that I formatted by CSS, it is populated by
my JavaScript.

RootPanel.get("slot1").add(dockPanel);

----

My WEB-INF/GWTSpring-servlet.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop
key="/activityHierarchy.rpc">ActivityHierarchyController</prop>
</props>
</property>
</bean>

<bean id="ActivityHierarchyController"
class="com.XYZ.web.activity.ActivityHierarchyController" />
</beans>


----

Service Interface


package com.XYZ.client;
import com.google.gwt.user.client.rpc.RemoteService;
public interface ActivityHierarchyService extends RemoteService
{
String fetchActivityHierarchy(int workflowId);
}


----
Async Interface in the client code:

package com.XYZ.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface ActivityHierarchyServiceAsync
{
public void fetchActivityHierarchy(int workflowId, AsyncCallback
callback);
}


----

Client Java code calling the service:


void fetchActivityHierarchy()
{
ActivityHierarchyServiceAsync service =
(ActivityHierarchyServiceAsync)
GWT.create(ActivityHierarchyService.class);
AsyncCallback callback = new AsyncCallback()
{
public void onSuccess(Object result)
{
Window.alert("AsyncCallback received response: " + result);
}

public void onFailure(Throwable caught)
{
Window.alert("Executing AsyncCallback: " + caught.toString());
}
};
ServiceDefTarget endpoint = (ServiceDefTarget) service;
String servletPath = "activityHierarchy.rpc";
Window.alert("Calling servlet at path: " + servletPath);
endpoint.setServiceEntryPoint(servletPath);
service.fetchActivityHierarchy(1, callback);
}


----

Controller for Spring Servlet (simplified for clarity), please notice:
com.XYZ.client.ActivityHierarchyService
that is my own service interface, here is required for the call to
work, I had to copy it from my GWT project to J2EE src

import org.gwtwidgets.server.spring.GWTSpringController;
public class ActivityHierarchyController extends GWTSpringController
implements ActivityHierarchyService
{
public String fetchActivityHierarchy(int workflowId)
{
return "Hello from the server!";
}
}


----
WEB-INF/web.xml got the following code inserted:

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

<servlet-mapping>
<servlet-name>GWTSpring</servlet-name>
<url-pattern>*.rpc</url-pattern>
</servlet-mapping>

----

I think this is all, I hope it helps somebody!

georgeuoa

unread,
Jan 23, 2007, 7:48:32 AM1/23/07
to Google Web Toolkit
... and would have been a tat more simple had you used the GWTHandler
from the same library instead of the GWTSpringController which allows
you to write a POJO service instead of extending the controllor:

public class ActivityHierarchyServiceImpl


implements ActivityHierarchyService
{
public String fetchActivityHierarchy(int workflowId)
{
return "Hello from the server!";
}

}

and in your servlet.xml :

<bean id="activityHierarchyService"
class="ActivityHierarchyServiceImpl"/>

<bean id="urlMapping" class="org.gwtwidgets.server.spring.GWTHandler">
<property name="mapping">
<map>
<entry key="/activityHierarchy.rpc"
value-ref="activityHierarchyService" />
</map>
</property>
</bean>

calos

unread,
Feb 21, 2007, 5:00:00 AM2/21/07
to Google Web Toolkit
hii,

i have developed on similar note d application of mine, d problem is
tat, my controller class is not found, also the point u said, to copy
it to J2EE folder, i think my prob is also there, but can u put more
light on it. as m deployin it on tomcat, n d structure is like:

appname/webpages(GWT compiled)
appname/WEB-INF/classes/(all classes)
appname/WEB-INF/lib/(required jar files)
appname/WEB-INF/xml files.. (d required xml files).

now where shld i copy my controller???

thanks in advance..

Reply all
Reply to author
Forward
0 new messages