Support for Developers of Spring Web Flow 2

Exclusive offer: get 50% off this eBook here
Spring Web Flow 2 Web Development

Spring Web Flow 2 Web Development — Save 50%

Master Spring's well-designed web frameworks to develop powerful web applications

$23.99    $12.00
by Markus Stäuble Sven Lüppken | March 2009 | Java Open Source Web Development

The primary focus of Support for Developers of Spring Web Flow is to introduce tools: build systems and IDEs. This will help developers build web applications using Spring Web Flow. This article by Sven Lüppken, shows you more about the Ant and the Maven build systems. We will also see how we can add support for the Spring Framework in the Eclipse IDE and include Spring NetBeans modules in the NetBeans IDE.

Build systems

Build systems are not necessary for building web applications with Spring Web Flow, but they greatly assist a developer by resolving dependencies between packages and automating the build process. In this article, we will show you how to build your projects with Apache Ant and Apache Maven.

Ant

Ant is a powerful and very flexible build tool. You can write Extensible Markup Language (XML) files, which tell Ant how to build your application, where to find your dependencies, and where to copy the compiled files. Often, you won't find the need to download Ant, as it is already built-in into popular IDEs such as Eclipse and NetBeans. Ant does not provide you with an automatic dependency resolving mechanism. So you will have to manually download all the libraries your application needs. Alternatively, you can use a third-party dependency resolving system such as Apache Ivy, which we will describe later in this article. When you have obtained a copy of Ant, you can write a build.xml file as shown in the following code.

<?xml version="1.0" encoding="UTF-8"?>
<project name="login.flow" default="compile">
<description>
login.flow
</description>
<property file="loginflow.properties"/>
<path id="classpath">
<fileset dir="lib/">
<include name="*.jar" />
</fileset>
</path>
<target name="init">
<mkdir dir="${build}" />
<mkdir dir="${build}/WEB-INF/classes" />
</target>
<target name="assemble-webapp" depends="init">
<copy todir="${build}" overwrite="y">
<fileset dir="${webapp-src}">
<include name="**/*/" />
</fileset>
</copy>
</target>
<target name="compile" depends="assemble-webapp">
<javac srcdir="${src}" destdir="${build}/WEB-INF/classes">
<classpath refid="classpath" />
</javac>
<echo>Copying resources</echo>
<copy todir="${build}/WEB-INF/classes" overwrite="y">
<fileset dir="${resources}">
<include name="**/*/" />
</fileset>
</copy>
<echo>Copying libs</echo>
<copy todir="${build}/WEB-INF/lib" overwrite="y">
<fileset dir="lib/">
<include name="*.jar" />
</fileset>
</copy>
</target>
</project>

First of all, we will specify that we have defined a few required folders in an external PROPERTIES file. The loginflow.properties, stored in your project's root folder, looks like this:

src = src/main/java
webapp-src = src/main/webapp
resources = src/main/resources
build = target/chapter02

These define the folders where your source code lies, where your libraries are located, and where to copy the compiled files and your resources. You do not have to declare them in a PROPERTIES file, but it makes re-using easier. Otherwise, you will have to write the folder names everywhere. This would make the build script hard to maintain if the folder layout changes.

In the init target, we create the folders for the finished web application. The next is the assemble-webapp target, which depends on the init target. This means that if you execute the assemble-webapp target, the init target gets executed as well. This target will copy all the files belonging to your web application (such as the flow definition file and your JSP files) to the output folder.

If you want to build your application, you will have to execute the compile target. It will initialize the output folder, copy everything your application needs to it, compile your Java source code, and copy the compiled files, along with the dependent libraries.

If you want to use Apache Ivy for automatic dependency resolution, first, you have to download the distribution from http://ant.apache.org/ivy. This article refers to Version 2.0.0 Release Candidate 1 of Ivy. Unpack the ZIP file and put the ivy-2.0.0-rc1.jar file in your %ANT_HOME%lib folder. If you are using the Eclipse IDE, Ant is already built into the IDE. You can add the JAR file to its classpath by right-clicking on the task you want to execute and choosing Run As | Ant Build…

Spring Web Flow 2 Web Development

In the appearing dialog, you can add the JAR file on the Classpath tab, either by clicking on Add JARs… and selecting a file from your workspace, or by selecting Add External JARs…, and looking for the file in your file system.

Spring Web Flow 2 Web Development

Afterwards, you just have to tell Ant to load the required libraries automatically by modifying your build script. We have highlighted the important changes (to be made in the XML file) in the following source code:

<project 
xmlns:ivy="antlib:org.apache.ivy.ant"
name="login.flow"
default="compile">
...
<target name="resolve"
description="--> retrieve dependencies with ivy">
<ivy:retrieve />
</target>
...
</project>

The last step, before we can actually build the project, involves specifying which libraries you want Ivy to download automatically. Therefore, we will now have to compose an ivy.xml file, stored in your project's root folder, which looks like this:

<ivy-module version="2.0">
<info organisation="com.webflow2book" module="login.flow"/>
<dependencies>
<dependency org="org.springframework.webflow"
name="org.springframework.binding" rev="2.0.5.RELEASE" />
<dependency org="org.springframework.webflow"
name="org.springframework.js" rev="2.0.5.RELEASE" />
<dependency org="org.springframework.webflow"
name="org.springframework.webflow" rev="2.0.5.RELEASE" />
</dependencies>
...
</ivy-module>

To keep the example simple, we only showed the Spring Web Flow entries of the file we just mentioned. In order to be able to build your whole project with Apache Ivy, you will have to add all other required libraries to the file. The org attribute corresponds to the groupId tag from Maven, as does the name attribute with the artifactId tag. The rev attribute matches the version tag in your pom.xml.

Maven

Maven is a popular application build system published by the Apache Software Foundation. You can get a binary distribution and plenty of information from the project's web site at http://maven.apache.org. After you have downloaded and unpacked the binary distribution, you have to set the M2_HOME environment variable to point to the folder where you unpacked the files. Additionally, we recommend adding the folder %M2_HOME%bin (on Microsoft® Windows system) or $M2_HOME/bin (on Unix or Linux systems) to your PATH variable.

Maven has a configuration file called settings.xml, which lies in the M2_HOMEconf folder. Usually, you do not edit this file, unless you want to define proxy settings (for example, when you are in a corporate network where you have to specify a proxy server to access the Internet), or want to add additional package repositories.

There are several plug-ins for the most popular IDEs around, which make working with Maven a lot easier than just using the command line. If you do not want to use a plug-in, you have to at least know that Maven requires your projects to have a specific folder layout. The default folder layout looks like this:

Spring Web Flow 2 Web Development

The root folder, directly below your projects folder, is the src folder. In the main folder, you have all your source files (src/main/java), additional configuration files, and other resources you need (src/main/resources), and all JSP and other files you need for your web application (src/main/webapp). The test folder can have the same layout, but is used for all your test cases. Please see the project's website for more information on the folder layout.

To actually build a project with Maven, you need a configuration file for your project. This file is always saved as pom.xml, and lies in the root folder of your project. The pom.xml for our example is too long to be included in this article. Nevertheless, we want to show you the basic layout. You can get the complete file from the code bundle uploaded on http://www.packtpub.com/files/code/5425_Code.zip.

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webflow2book</groupId>
<artifactId>chapter02</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>chapter02 Maven Webapp</name>
<url>http://maven.apache.org</url>

This is a standard file header where you can define the name and version of your project. Further, you can also specify how your project is supposed to be packaged. As we wanted to build a web application, we used the war option. Next, we can de?ne all the dependencies our project has to the external libraries:

<dependencies>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>org.springframework.binding</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>org.springframework.js</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>org.springframework.webflow</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
...
</dependencies>

As you can see, defining a dependency is pretty straightforward. If you are using an IDE plug-in, the IDE can do most of this for you. To build the application, you can either use an IDE or open a command-line window and type commands that trigger the build. To build our example, we can enter the projects folder and type:

mvn clean compile war:exploded

This cleans up the target folder, compiles the source files, and compiles all necessary files for our web application in the target folder. If you use Tomcat, you can point your context docBase to the target folder. The application will be automatically deployed on the startup of Tomcat, and you can test your application.

Spring Web Flow 2 Web Development Master Spring's well-designed web frameworks to develop powerful web applications
Published: March 2009
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

IDE

There are several very good IDEs available for Java developers. For this article, we choose to explain two of the most popular ones: Eclipse and NetBeans.

Eclipse and Spring IDE

If your favorite IDE is Eclipse, you can use Spring IDE for adding support for the Spring Framework in general and, of course, for Spring Web Flow. Spring IDE is an open source project. You can get more information about the project, including a description of its features, on http://springide.org. Spring IDE uses the standard Eclipse update site mechanism to distribute the releases. In our examples, the IDE used is Eclipse, Ganymede (3.4). We will show you how to install the Spring IDE step-by-step:

  1. First of all, you have to choose the Software Updates… entry from the Help menu.

    Spring Web Flow 2 Web Development

  2. In the Software Updates and Add-ons dialog, choose the Available Software tab and click on the Add Site... button.

    Spring Web Flow 2 Web Development

  3. Enter the URL of the update site (http://springide.org/updat esite) in the Add Site dialog and click OK. The dialog closes, and you will see the Software Updates and Add-ons dialog again, this time with the new update site added. (This site is called Spring IDE Update Site.)
  4. Make sure you check at least those checkboxes in front of the required update (Spring IDE Core) and the Web Flow support (Spring IDE Web Flow Extension), as shown in the following screenshot. Feel free to choose more features from the list.

    Spring Web Flow 2 Web Development

  5. Click on the Install… button. Eclipse will start to resolve any dependencies on the Spring IDE packages you have chosen. You will be prompted again to confirm your choice, and you will have to accept the license text of the Spring IDE project. When you click the Finish button, Eclipse will download and install all the packages you have chosen.
  6. Eclipse  will prompt you afterwards as to whether you want to restart the IDE to apply the changes. We recommend choosing the Yes option. Eclipse will restart and you will be ready to use the features the Spring IDE add-on provides.

    Spring Web Flow 2 Web Development

In case you cannot use the update site, or you want to use an older version of the plug-in, you can always use the archived update site.  You just have to download the release you want to use directly from the web site http://springide.org/updatesite. Please note that you have to append the release you want to download to this URL. For example, the current release of Spring IDE can be downloaded by using the URL: http://springide.org/updatesite/spring-ide_updatesite_2.2.1_v200811281800.zip. Then you tell Eclipse to use this repository instead of the standard update site. You can do this by choosing the Archive… button in the Add Site dialog and selecting the downloaded archive file:

Spring Web Flow 2 Web Development

In order to test your new add-on, you can try to open up a new Spring project. If the plug-in is installed successfully, you should have a new entry in the New Project wizard, regardless of the type of installation you've chosen, from the update site or the archived version:

Spring Web Flow 2 Web Development

 

The project will already have the so-called Spring Nature enabled, which enables all the features of Spring IDE for the selected project. You can always add the Spring Nature to any project by right-clicking on the project name in the Project Explorer and selecting the Spring Tools | Add Spring Project Nature option in the pop-up menu. After you've added the Spring Nature to your project, you will have a new Spring entry in the projects' properties dialog. There are two sub-elements of the Spring entry:

  1. Beans Support: You can add your Spring configuration files here. Later, they will be managed automatically by Spring IDE. For our example application, we would add the firstexample-servlet.xml file (which includes all our bean definitions) to this dialog:

    Spring Web Flow 2 Web Development

  2. Web Flow Support: Here you can add your flow definition files. As with the Spring configuration files, your flow definition files will be automatically managed by Spring IDE afterwards:

    Spring Web Flow 2 Web Development

You get plenty of convenient features that make it much easier to develop your applications if you let Spring IDE handle your configuration files: for example, your references to beans are checked automatically. If you specify a reference to a bean which does not exist, Eclipse will warn you to check your configuration. Additionally, you will get auto-completion support for the Spring configuration files. For a full list of features that Spring IDE provides, please visit the project's web site at http://springide.org.

NetBeans

Starting with the 6.1 release of NetBeans, the application includes the Spring NetBeans module. To make sure it is present in your installation, open the Plugins dialog using Tools | Plugins. Then switch to the Installed tab. You should see an entry called Spring Web MVC.

 

Spring Web Flow 2 Web Development

If it is not installed, it maybe because you've downloaded the Java SE version of NetBeans, which does not include any J2EE technologies; you can download the plug-in using the Available Plugins tab:

  1. If you haven't done so already, open the Plugins dialog using Tools | Plugins
  2. Switch to the Available Plugins tab and scroll down through the list until you find the entry Spring Web MVC. You will also need the Java EE module and the appropriate module for your application server. This means that you will need the Tomcat module if you want to use Apache Tomcat. Tick the checkboxes next to all the modules you want to install, and click on the Install button.

    Spring Web Flow 2 Web Development

  3. The NetBeans IDE Installer dialog opens up, which will show you the plug-ins that are going to be installed. When you click on the Next button, you will be presented with the license agreement.
  4. When you accept them and click on the Install button, NetBeans will begin installing the add-ons. Afterwards you will be prompted to restart NetBeans, and we recommend you do it.

After you have successfully installed the Spring NetBeans module, you can create a new project using it. To do this, open the New Project wizard and click on the Web Application template, to be found under the Web category, and then click on the Next button.

Spring Web Flow 2 Web Development

Now you have to enter a name for your project and the folder path where it should be saved. A further click on Next, and you will be presented with a dialog, where you can choose your application server. In case you have not already added one before, you can click on the Add… button to create one.

In the next dialog, you can select the frameworks you want to use in your web application. You'll find the option Spring Web MVC 2.5 listed in the dialog. Select it and click on Finish.

Spring Web Flow 2 Web Development

The Spring NetBeans module does not include explicit support for Spring Web Flow, but has several other very convenient features, which make the life of the developer much easier. Please see the project's web site for a full description of the various features. You can find information on the plug-in at http://wiki.netbeans.org/SpringSupport.

Summary:

This article explains the tools that exist to support developers with their daily work with Spring Web Flow. This article describes Ant as a powerful and very flexible build tool and shows how we can write a build.xml file, that tells Ant how to build your application, where to find your dependencies, and where to copy the compiled files. We also write an ivy.xml file which specifies the libraries we want Ivy to download automatically. This article shows the basic layout of the pom.xml which is used to build a project with Maven. It shows a step-by-step procedure to install Spring IDE in Eclipse and to add Spring Nature to a existing folder. The article covers how to include the Spring Module in NetBeans.

If you have read this article you may be interested to view :

 

Spring Web Flow 2 Web Development Master Spring's well-designed web frameworks to develop powerful web applications
Published: March 2009
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Markus Stäuble

Markus Stäuble is currently working as CTO at namics (deutschland) gmbh. He has a Master degree in Computer Science. He started programming with Java in the year 1999. After that he has earned much experience in building enterprise java systems, especially web applications. He has a deep knowledge of the java platform and the tools and frameworks around Java.

Sven Lüppken

Sven Lüppken has a degree in computer science, which he passed with distinction. He is currently employed as Java Software Developer at one of the leading broadcasting and production companies in Germany. Sven started programming in C and C++ at the age of 16 and quickly fell in love with the Java programming language during his studies.

When he got the chance to write his diploma thesis about object-relational mapping technologies he accepted at once.

Since then, he has integrated Hibernate and the JPA in many projects, always in conjunction with the Spring framework.

Books From Packt

 

Spring 2.5 Aspect Oriented Programming
Spring 2.5 Aspect Oriented Programming

Learning jQuery 1.3
Learning jQuery 1.3

Drupal 6 Site Builder Solutions
Drupal 6 Site Builder Solutions

JBoss Portal Server Development
JBoss Portal Server Development

Drupal 6 Social Networking
Drupal 6 Social Networking

WordPress Plugin Development: Beginner's Guide
WordPress Plugin Development: Beginner's Guide

Java EE 5 Development with NetBeans 6
Java EE 5 Development with NetBeans 6

jQuery UI 1.6: The User Interface Library for jQuery
jQuery UI 1.6: The User Interface Library for jQuery

 

 

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
j
K
H
H
8
C
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software