You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Next »

The Struts 2 Blank Archetype ("blank-archetype") provides a minimal, but complete, Struts 2 application. It demonstrates some of the most basic Struts 2 concepts.

Features

  • XML-based configuration, demonstrates including additional config file
  • Example actions
  • Package-level resource bundle
  • XML-based validation
  • Unit-testing

Contents

Creating Our blank-archetype Project

We'll run the following command from our project's parent directory (this is shown using Unix-style commands).

$ mvn archetype:generate -B \
                         -DgroupId=tutorial \
                         -DartifactId=tutorial \
                         -DarchetypeGroupId=org.apache.struts \
                         -DarchetypeArtifactId=struts2-archetype-blank \
                         -DarchetypeVersion=2.1.6
$ ls
tutorial/
$ cd tutorial
$ ls
pom.xml         src/

Depending on the state of your local system you may see Maven downloading various libraries (known as "downloading the internet", which is what it seems Maven does sometimes). Be patient--Maven is basically setting up your required libraries automatically.

Project Structure

The source code structure follows the normal Maven directory structure. The blank-archetype does not include all of the directories listed in the Maven structure reference page.

Our project's structure looks like this:

Directory

Description

src

All project source

¦- main

Primary source directory

¦  ¦- java

Java source code

¦  ¦  `- tutorial

Package defined by groupId parameter

¦  ¦     `- example

The example actions from the archetype

¦  ¦- resources

Resources (config, property, and validation files, and so on

¦  ¦  `-tutorial

Package defined by groupId parameter

¦  ¦     `- example

Example property and validation files from archetype

¦  `- webapp

Web application files (HTML, JSP, etc.)

¦      `- WEB-INF

Typical WEB-INF folder

¦         `- example

Files from archetype

`- test

Test code (unit tests etc.)

   `- java

Java-based test code

      `- tutorial

Package defined by groupId parameter

         `- example

Test code from archetype

Structure Difference From Non-Maven Projects

One big change for folks not used to the Maven structure is the separation of Java source code and resource files. For example, in a non-Maven project our directory structure might look like this:

src

All project source

¦- tutorial

Tutorial package

¦  `- example

Example package

¦     ¦- Login.java

Login action source

¦     ¦- package.properties

Resource file

¦     `- Login-validation.xml

Validation file

`- struts.xml

Struts 2 config file

web

Web app files

¦- WEB-INF

Typical WEB-INF folder

`- index.html

An HTML file

It can take a little while to get used to, but ultimately it provides good separation of "types" of things, and becomes second-nature pretty quickly. Note that it's possible to use a non-Maven directory layout with Maven projects, but this can be challenging at some points.

Building The Project

There are several different ways we can go about building our project, from simple compilation up to actually running the web application--all from within Maven.

Compilation

$ mvn compile

will create a target directory containing the compiled classes. By itself this isn't terribly useful.

Testing

Running

$ mvn test

will compile the application and run the included unit tests. Blank-archetype's unit tests are not extensive, but provide a simple starting point for writing more complex, application-specific tests.

One we've run the Maven test command we'll notice there's a target/surefire-reports directory. The Maven Surefire Plugin is how Maven runs our unit tests. By default it will create test results in XML and text formats in the target/surefire-reports directory. These files can be examined to get further information regarding the failed tests.

Assembling (Creating a WAR)

Running

We can run blank-archetype using the Jetty server via the Maven Jetty Plugin by executing the Jetty plugin's run command:

$ mvn jetty:run

Once we've run the application we can see that it works by visiting localhost:8080/tutorial/example/HelloWorld.action as a sanity check.

Application Documentation

The application consists of a few test actions demonstrating simple validation and package-level property (resource) files.

  • No labels