Preface
Struts Action Framework 2 is a popular, easy-to-use MVC framework. This tutorial will help you get started with the framework, even if you are not an experienced Java web application developer. The tutorial will walk you The Bootstrap tutorial walks through installing the framework and creating a simple application.
Note |
---|
...
While the framework is |
...
simple to use, creating non-trivial applications |
...
assumes a working knowledge of |
...
Panel | |
---|---|
title | Supporting Technologiesmany JEE technologies, including:
|
For more about any of the supporting technologies, visit see the Key Technologies page on the Apache Struts websitePrimer.
In this the Ready, Set, Go! lesson, we download the framework and get started on an application of our own.
Download the Distribution
...
A
...
The full distribution can be downloaded form from the Apache Struts website. The full distribution contains the strutsstruts2-action2core.jar
file,related dependencies, example applications, a copy of all the documentation , sources, all required and optional dependencies,and example applications. For more information on how to build the framework from source or even from a clean checkout, please in HTML format, and the complete source code.
Tip |
---|
To compile it yourself, refer to Building the Framework from Source. |
...
Our First
...
Application
To get started on your own with a new application, you we can utilize use the blank template, run the Ant taskMaven archetype, or just setup your own set up a web infrastructure from scratch.
Using the Blank
...
Application
The blank web application in the distribution's webapp
apps
directory is meant as a template. Make We can make a copy of the "blank.war", deploy it to our container, and use the exploded copy as the basis for your our application.
Run the Ant task
While it's simple enough to copy a directory tree, there is an even easier way to get started. In the webapps
directory, there is a build.xml
file. The new
task of this Ant build file can create fresh, empty web application for you. Just add content!
Code Block |
---|
> cd /projects/Apache/struts-current/action2/webapps
> ant new
|
The Ant buildfile will prompt you for a the name of your new application.
No Format |
---|
new:
[echo]
[echo] +=============================================================+
[echo] | -- Create a new web application -- |
[echo] +=============================================================+
[echo]
[input] Enter the name of your new application [myapp]? saf-sample
[echo] Creating 'saf-sample' web application...
[copy] Copying 7 files to /Users/rainerh/projects/action2/webapps/saf-sample
[copy] Copying 1 file to /Users/rainerh/projects/action2/webapps/saf-sample
[echo]
[echo] +=============================================================+
[echo] | -- Your Web Application was created successfully! -- |
[echo] | |
[echo] | Now you should be able to cd to your application and run: |
[echo] | > ant build -Dwebapp=saf-sample |
[echo] +=============================================================+
BUILD SUCCESSFUL
|
This task creates a new directory within the webapps dir.
For example, this is the setup for a new webapp project created with ant new
webapp name saf-sample
:
No Format |
---|
webapps/
saf-sample/
src/
java/
org/apache/struts/action2/example/HomeAction.java -- A simple action example implementation
webapp/
index.jsp -- redirects to home.action
WEB-INF/
classes/
action.properties -- Simple properties to use Spring and run SAF in devMode
action.xml -- Basic action mapping sample with one action mapping
pages/
home.jsp -- The home.jsp referenced via the HomeAction
applicationContext.xml -- blank Spring definition file. Add your Spring beans here.
web.xml -- basic web.xml for SAF
|
You can now use the newly created project structure to get your SAF-based project running.
There is even a simple batch file in the source code directory that we can use to recompile the application in place.
Run the Maven Archetype
For those of us using Maven as a build system, we can use the Maven Archetype to create a new application.
For directions on using the Struts 2 archetypes see the Struts 2 Maven Archetypes page.
Setting up from scratch
If for some reason the blank template or archetype doesn't work for youout, it's not so hard to setup a SAF-based Struts 2 application from scratch.
Structure of your web application
- Setup a file structure for your application.
Code Block |
---|
/mywebapp/
/mywebapp/template/
/mywebapp/META-INF/
/mywebapp/WEB-INF/
/mywebapp/WEB-INF/classes/
/mywebapp/WEB-INF/lib/
/mywebapp/WEB-INF/lib/CORE&OPTIONAL *.jar
/mywebapp/WEB-INF/web.xml
|
- Copy to your
webapp/lib
directory- the
struts-action-(VERSION).jar
, - all the
*.jar
files in/lib/default
, and - any necessary optional
*.jar
files from/lib/
.
- the
If you need to customize your own templates (how HTML is rendered from webwork UI tags), copy into your
webapp
directory the /src/java/template
directory.
Minimum set of libraries and configuration files
The following files are a minium requirement for your application.
Filename | Description |
---|---|
| Framework library itself, found in distribution root directory |
| XWork library on which SAF 2 is built |
| OSCore, a general-utility library from OpenSymphony |
| Object Graph Navigation Language (OGNL), the expression language used throughout the framework |
| Commons logging, which the framework uses to support transparently logging to either Log4J or JDK 1.4+ |
| All UI tag templates are written in Freemarker, which is also a good option for your views |
| The default dependency injection container for the framework. |
| J2EE web application configuration file that defines the servlets, JSP tag |
| Framework configuration file that defines the actions, results, and interceptors for your application |
The library files (*.jar
) needs to be copied to your /mywebapp/WEB-INF/lib/
directory. If you need optional functionalities requiring dependencies on optional JARs, those JARs need to be copied to this directory too.
Setup web.xml
Wiki Markup |
---|
Create an {{web.xml}} file in {{\[webapp\]/WEB-INF}} (or merge into it the framework resources). |
Code Block | ||
---|---|---|
| ||
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Application</display-name>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts.action2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
|
The standard web.xml
registers a FilterDispatcher to enable framework functionality for your requests. The ContextLoaderListener configures Spring as our dependency injection conitainer. The framework uses Spring internally, and you may wish to use it to deploy your own objects.
See also: web.xml
Setup action.xml
Create a skeleton action.xml
file in /$APP/WEB-INF/classes
.
Code Block | ||
---|---|---|
| ||
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork> <!-- Include framework defaults (from Struts Action JAR). -->
<include file="action-default.xml" />
<!-- Configuration for the default package. -->
<package name="default" extends="action-default">
</package>
</xwork>
|
For now, the action.xml
does only two things:
- It tells the framework that it should import the configuration information from
action-default.xml
. (This file is located at the root of thestruts-action.jar
, so it is sure to be found.)
- It defines a default package (with the <package> section) where framework elements like
actions
,results
andinterceptors
are registered.
See also: action.xml
For details, see Simple Setup.
Next | Onward to Hello World |
---|---|
Prev | Return to Step by Step Bootstrap |