Sunday, October 23, 2011

Spring and GWT tutorial. Part 1 - Spring Configuration

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. Let's begin by setting Spring configuration.

This post has a Russian version.

We will use next technologies:
  • Java 8
  • Spring 4.2.4
  • GWT 2.7.0
  • Maven 3.3.3
  • Tomcat 8.0.30

We will use Maven to build a project.
You can read there about Maven settings.

pom.xml contains description of all artifacts that our application requires. Now we need only spring-web artifact, which contains all necessary Spring libraries:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hellogwt</groupId>
    <artifactId>hellogwt</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>hellogwt</finalName>
    </build>

</project>

Deployment descriptor web.xml contains configuration of Spring application context, Dispatcher servlet and welcome page:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    <servlet-mapping>
        <servlet-name>hellogwt</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

File applicationContext.xml is needed to create application context. It will contain all necessary beans declarations and settings. We don't have any of them at this point, but the file must exist:
<?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.xsd">

</beans>

Let's create a welcome page index.jsp in webapp folder:
<html>
    <body>
        Hello, Spring!
    </body>
</html>

Build the project by executing  "mvn clean install" command:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hellogwt 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hellogwt ---
[INFO] Deleting D:\Work\hellogwt\target
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ hellogwt ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Work\hellogwt\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hellogwt ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ hellogwt ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Work\hellogwt\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hellogwt ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ hellogwt ---
[INFO] No tests to run.
[INFO] Surefire report directory: D:\Work\hellogwt\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ hellogwt ---
[INFO] Packaging webapp
[INFO] Assembling webapp [hellogwt] in [D:\Work\hellogwt\target\hellogwt]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\Work\hellogwt\src\main\webapp]
[INFO] Webapp assembled in [53 msecs]
[INFO] Building war: D:\Work\hellogwt\target\hellogwt.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ hellogwt ---
[INFO] Installing D:\Work\hellogwt\target\hellogwt.war to C:\Users\Alex\.m2\repository\hellogwt\hellogwt\1.0\hellogwt-1.0.war
[INFO] Installing D:\Work\hellogwt\pom.xml to C:\Users\Alex\.m2\repository\hellogwt\hellogwt\1.0\hellogwt-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.860s
[INFO] Finished at: Wed Oct 19 21:15:08 EEST 2011
[INFO] Final Memory: 6M/245M
[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:











Folder structure:






















Application source code is available at https://subversion.assembla.com/svn/hellogwt/trunk/.

The skeleton of our application is ready. Next we will add some GWT :)

2 comments:

  1. Thanks for this working and easy to use - understand tutorial ! : )

    ReplyDelete