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!
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>
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..