Monday, October 24, 2011

Spring and GWT tutorial. Part 2 - Adding GWT

Recently I've started working in a GWT project. It is a new technology for me, so I've decided to create a simple web application based on Spring and GWT.

This post has a Russian version.

If you have never heard about GWT, you can read about it there and pass a very useful tutorial.

We have already created a skeleton of our application using Spring. Now let's add GWT to our project.

At first, let's change pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""





We will use spring4gwt library to integrate Spring with GWT. It is not contained in Maven repository, so we have to download and add it to WEB-INF/lib project's folder.

UPD: Instead of manually adding spring4gwt library to the project we can add it to Maven local repository and set that our project depends on it. We have to:
  • execute next command in directory where library is located:
    mvn install:install-file -Dfile=spring4gwt-0.0.1.jar -DartifactId=spring4gwt -Dversion=0.0.1 -Dpackaging=jar
  • add a new dependency into pom.xml:
Anton, thanks for a good advice!

Also, we will make some changes in web.xml:
  • replace DispatcherServlet with SpringGwtRemoteServiceServlet from spring4gwt
  • replace servlet's url-pattern with "/hellogwt/springGwtServices/*" for correct functioning of application services
  • replace welcome page with HelloGWT.html
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""





Rename index.jsp to HelloGWT.html and change its contents:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="hellogwt.css">
    <script type="text/javascript" language="javascript" src="hellogwt/hellogwt.nocache.js"></script>
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1'

Create a xml module file HelloGWT.gwt.xml in the com.hellogwt package:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6.2//EN"

<module rename-to='hellogwt'>
    <inherits name=''/>
    <inherits name=''/>
    <entry-point class='com.hellogwt.client.HelloGWT'/>

After that create HelloGWT class in the com.hellogwt.client package. Our method onModuleLoad() will add a label with text "Hello, GWT!" into html page:
package com.hellogwt.client;


public class HelloGWT implements EntryPoint {

    public void onModuleLoad() {
        Label greetingLabel = new Label("Hello, GWT!");

Build the project by executing  "mvn clean install" command:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building hellogwt 1.0
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hellogwt ---
[INFO] Deleting d:\Work\blog\hellogwt-2\target
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellogwt ---
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory d:\Work\blog\hellogwt-2\src\main\resources
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hellogwt ---
[WARNING] File encoding has not been set, using platform encoding Cp1251, i.e. build is platform dependent!
[INFO] Compiling 1 source file to d:\Work\blog\hellogwt-2\target\classes
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hellogwt ---
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory d:\Work\blog\hellogwt-2\src\test\resources
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hellogwt ---
[INFO] Nothing to compile - all classes are up to date
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hellogwt ---
[INFO] No tests to run.
[INFO] --- gwt-maven-plugin:2.4.0:compile (default) @ hellogwt ---
[INFO] auto discovered modules [com.hellogwt.HelloGWT]
[INFO] Compiling module com.hellogwt.HelloGWT
[INFO]    Compiling 6 permutations
[INFO]       Compiling permutation 1...
[INFO]       Compiling permutation 3...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 0...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 2...
[INFO]       Compiling permutation 4...
[INFO]       Compiling permutation 5...
[INFO]    Compile of permutations succeeded
[INFO] Linking into d:\Work\blog\hellogwt-2\target\hellogwt\hellogwt
[INFO]    Link succeeded
[INFO]    Compilation succeeded -- 29.131s
[INFO] --- maven-war-plugin:2.2:war (default-war) @ hellogwt ---
[INFO] Packaging webapp
[INFO] Assembling webapp [hellogwt] in [d:\Work\blog\hellogwt-2\target\hellogwt]
[INFO] Processing war project
[INFO] Copying webapp resources [d:\Work\blog\hellogwt-2\src\main\webapp]
[INFO] Webapp assembled in [199 msecs]
[INFO] Building war: d:\Work\blog\hellogwt-2\target\hellogwt.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] --- gwt-maven-plugin:2.4.0:test (default) @ hellogwt ---
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hellogwt ---
[INFO] Installing d:\Work\blog\hellogwt-2\target\hellogwt.war to C:\Users\Alex\.m2\repository\hellogwt\hellogwt\1.0\hellogwt-1.0.war
[INFO] Installing d:\Work\blog\hellogwt-2\pom.xml to C:\Users\Alex\.m2\repository\hellogwt\hellogwt\1.0\hellogwt-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.302s
[INFO] Finished at: Sun Apr 13 00:28:11 EEST 2014
[INFO] Final Memory: 23M/224M
[INFO] ------------------------------------------------------------------------

Now we have application war-file. Copy it into Tomcat's webapps folder, start the server and go to http://localhost:8080/hellogwt/. Our welcome page should be displayed with text "Hello, GWT!":

Folder structure:

Application source code is available at

We did it! Now Spring and GWT can work together. Next we will add GWT RPC services to our application.

No comments:

Post a Comment