Friday, August 21, 2009




I started doing some test projects trying to integrate GWT with a simple Spring bean. The spring bean is a Greeting service generated by the eclipse gwt plug-in. I removed the RemoteServiceServlet reference in GreetingServiceImpl and instead chose the servlet provided by the Spring4GWT library to make the spring bean available as a servlet. To get the following directory structure I used the eclipse gwt plug-in and created a new web application project.

This is my project hierarchy




This is my web.xml

<web-app>


<welcome-file-list>

<welcome-file>Spring4gwtEx.htmlwelcome-file>

welcome-file-list>

<context-param>

<param-name>contextConfigLocationparam-name>

<param-value>

/WEB-INF/applicationContext.xml

param-value>

context-param>

<servlet>

<servlet-name>springGwtRemoteServiceServletservlet-name>

<servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServletservlet-class>

servlet>

<servlet-mapping>

<servlet-name>springGwtRemoteServiceServletservlet-name>

<url-pattern>/springGwtServices/*url-pattern>

servlet-mapping>


web-app>


This is my applicationContext.xml

xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean id="myAspect" class="com.gs.spring4.gwt.ex.client.annotations.Introductions">

bean>

<bean id="greetingService" class="com.gs.spring4.gwt.ex.server.GreetingServiceImpl"/>

beans>


This is my GreetingService.java

package com.gs.spring4.gwt.ex.client;


import com.google.gwt.user.client.rpc.RemoteService;

import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;


/**

* The client side stub for the RPC service.

*/

@RemoteServiceRelativePath("springGwtServices/greetingService")

public interface GreetingService extends RemoteService {

String greet(String name);

}




This is my GreetingServiceImpl.java


package com.gs.spring4.gwt.ex.server;


import org.springframework.stereotype.Service;


import com.gs.spring4.gwt.ex.client.GreetingService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


/**

* The server side implementation of the RPC service.

*/

@SuppressWarnings("serial")

@Service("greetingService")

public class GreetingServiceImpl implements

GreetingService {


public String greet(String input) {

return "Hello from the server, " + input + "!";

}

}



This is my GreetingServiceAsync.java

package com.gs.spring4.gwt.ex.client;


import com.google.gwt.user.client.rpc.AsyncCallback;


/**

* The async counterpart of GreetingService.

*/

public interface GreetingServiceAsync {

void greet(String input, AsyncCallback callback);

}

This is my Aspect class

package com.gs.spring4.gwt.ex.client.annotations;


import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.DeclareParents;


import com.gs.spring4.gwt.ex.client.GreetingService;


@Aspect

public class Introductions {

@SuppressWarnings("unused")

@DeclareParents("GreetingServiceImpl")

private GreetingService greetingService;

}



This is my GWT client code

package com.gs.spring4.gwt.ex.client;


import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.core.client.GWT;

import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.event.dom.client.KeyCodes;

import com.google.gwt.event.dom.client.KeyUpEvent;

import com.google.gwt.event.dom.client.KeyUpHandler;

import com.google.gwt.user.client.rpc.AsyncCallback;

import com.google.gwt.user.client.rpc.ServiceDefTarget;

import com.google.gwt.user.client.ui.Button;

import com.google.gwt.user.client.ui.DialogBox;

import com.google.gwt.user.client.ui.HTML;

import com.google.gwt.user.client.ui.Label;

import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.user.client.ui.TextBox;

import com.google.gwt.user.client.ui.VerticalPanel;


/**

* Entry point classes define onModuleLoad().

*/

public class Spring4gwtEx implements EntryPoint {

/**

* The message displayed to the user when the server cannot be reached or

* returns an error.

*/

private static final String SERVER_ERROR = "An error occurred while "

+ "attempting to contact the server. Please check your network "

+ "connection and try again.";


/**

* Create a remote service proxy to talk to the server-side Greeting service.

*/

private final GreetingServiceAsync greetingService = GWT

.create(GreetingService.class);


/**

* This is the entry point method.

*/

public void onModuleLoad() {

final Button sendButton = new Button("Send");

final TextBox nameField = new TextBox();

nameField.setText("GWT User");


// We can add style names to widgets

sendButton.addStyleName("sendButton");


// Add the nameField and sendButton to the RootPanel

// Use RootPanel.get() to get the entire body element

RootPanel.get("nameFieldContainer").add(nameField);

RootPanel.get("sendButtonContainer").add(sendButton);


// Focus the cursor on the name field when the app loads

nameField.setFocus(true);

nameField.selectAll();


// Create the popup dialog box

final DialogBox dialogBox = new DialogBox();

dialogBox.setText("Remote Procedure Call");

dialogBox.setAnimationEnabled(true);

final Button closeButton = new Button("Close");

// We can set the id of a widget by accessing its Element

closeButton.getElement().setId("closeButton");

final Label textToServerLabel = new Label();

final HTML serverResponseLabel = new HTML();

VerticalPanel dialogVPanel = new VerticalPanel();

dialogVPanel.addStyleName("dialogVPanel");

dialogVPanel.add(new HTML("Sending name to the server:"));

dialogVPanel.add(textToServerLabel);

dialogVPanel.add(new HTML("
Server replies:"));

dialogVPanel.add(serverResponseLabel);

dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);

dialogVPanel.add(closeButton);

dialogBox.setWidget(dialogVPanel);


// Add a handler to close the DialogBox

closeButton.addClickHandler(new ClickHandler() {

public void onClick(ClickEvent event) {

dialogBox.hide();

sendButton.setEnabled(true);

sendButton.setFocus(true);

}

});

ServiceDefTarget endpoint = (ServiceDefTarget) greetingService;

endpoint.setServiceEntryPoint("../../springGwtServices/greetingService");


// Create a handler for the sendButton and nameField

class MyHandler implements ClickHandler, KeyUpHandler {

/**

* Fired when the user clicks on the sendButton.

*/

public void onClick(ClickEvent event) {

sendNameToServer();

}


/**

* Fired when the user types in the nameField.

*/

public void onKeyUp(KeyUpEvent event) {

if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {

sendNameToServer();

}

}


/**

* Send the name from the nameField to the server and wait for a response.

*/

private void sendNameToServer() {

sendButton.setEnabled(false);

String textToServer = nameField.getText();

textToServerLabel.setText(textToServer);

serverResponseLabel.setText("");

greetingService.greet(textToServer,

new AsyncCallback() {

public void onFailure(Throwable caught) {

// Show the RPC error message to the user

dialogBox

.setText("Remote Procedure Call - Failure");

serverResponseLabel

.addStyleName("serverResponseLabelError");

serverResponseLabel.setHTML(SERVER_ERROR);

dialogBox.center();

closeButton.setFocus(true);

}


public void onSuccess(String result) {

dialogBox.setText("Remote Procedure Call");

serverResponseLabel

.removeStyleName("serverResponseLabelError");

serverResponseLabel.setHTML(result);

dialogBox.center();

closeButton.setFocus(true);

}

});

}

}


// Add a handler to send the name to the server

MyHandler handler = new MyHandler();

sendButton.addClickHandler(handler);

nameField.addKeyUpHandler(handler);

}

}



During multiple server starts I realized I had to add the following libraries to the WEB-INF/lib path.
  • spring-context-2.5.6.jar
  • spring4gwt-0.0.1.jar
  • log4j-1.2.14.jar etc...

Now that I run the application I can get to the first page successfully.


But on click of the Send button I get an error and the following is the console message.

Following is the stacktrace.

The server is running at http://localhost:8080/

2009-08-23 07:43:14.083 java[3067:80f] [Java CocoaComponent compatibility mode]: Enabled

2009-08-23 07:43:14.084 java[3067:80f] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000

log4j:WARN No appenders could be found for logger (org.spring4gwt.server.SpringGwtRemoteServiceServlet).

log4j:WARN Please initialize the log4j system properly.

Aug 23, 2009 7:43:22 AM com.google.appengine.tools.development.ApiProxyLocalImpl log

SEVERE: [1251038602584000] javax.servlet.ServletContext log: Exception while dispatching incoming RPC call

java.lang.UnsupportedClassVersionError: Bad version number in .class file

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:675)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

at java.net.URLClassLoader.access$100(URLClassLoader.java:56)

at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:316)

at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:142)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)

at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:89)

at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:55)

at org.spring4gwt.server.SpringGwtRemoteServiceServlet.processCall(SpringGwtRemoteServiceServlet.java:31)

at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:86)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)

at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)

at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)

at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)

at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)

at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)

at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)

at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)

at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)

at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)

at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:306)

at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)

at org.mortbay.jetty.Server.handle(Server.java:313)

at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506)

at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:844)

at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:644)

at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)

at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381)

at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)

at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)



So it looks like there is a version compatibility problem with my compiled code. I think it requires java 1.6 but the eclipse gwt plug-in created project won't run unless I use 1.5.

IDE: Eclipse
java 1.5 as required by the gwt plug-in.