If for some reason the blank template or archetype doesn't work out, we can just setup an application from scratch.

Setup the Web Application File Structure

/tutorial/
/tutorial/META-INF/
/tutorial/WEB-INF/
/tutorial/WEB-INF/classes/struts.xml
/tutorial/WEB-INF/lib/
/tutorial/WEB-INF/lib/minimum JARs + any plugin JARs + plugin dependencies
/tutorial/WEB-INF/web.xml
  • Copy to your webapp/lib directory
    • the required JARs (see next section),
    • any Struts plugin JARs,
    • any plugin dependencies.

To customize the Struts templates (how HTML is rendered from the tags), copy into the application's webapp directory the framework's /src/java/template directory.

Install the Minimum Set of Libraries and Configuration Files

The following files are a minium requirement for your application.

Filename

Description

struts2-core.jar

Framework library itself, found in distribution root directory

xwork.jar

XWork 2 library on which Struts 2 is built (version 2.0 or later)

ognl.jar

Object Graph Navigation Language (OGNL), the expression language used throughout the framework

javassist.jar

Java bytecode manipulation library used by OGNL

freemarker.jar

All UI tag templates are written in Freemarker (also a good option for your own views)

commons-logging.jar

Commons logging, which the framework uses to support transparently logging to either Log4J or JDK 1.4+

commons-fileupload.jar

The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.

commons-io.jar

Commons IO is a library of utilities to assist with developing IO functionality.

commons-lang3.jar

Commons Lang3 is used to simplify usage of common tasks and code shortcuts to to stay DRY

web.xml

Java web application configuration file that defines the filters (and other components) for your web application

struts.xml

Framework configuration file that defines the actions, results, and interceptors for your application

If any Struts 2 Plugins are included, then other JARs may be needed too. For example, the optional Spring Plugin requires the Spring JARs to be present.

Setup the Web Application Deployment Descriptor (web.xml)

Create an web.xml file in [webapp]/WEB-INF (or merge into it the framework resources).

web.xml
<?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>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

The standard web.xml registers a FilterDispatcher to enable framework functionality for your requests.

If other packages are being used, like SiteMesh or Spring, then other filters may need to be configured too

(lightbulb) See also: web.xml

Setup the Struts Configuration (struts.xml)

Create a skeleton struts.xml file in /$APP/WEB-INF/classes.

struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts><!-- Configuration for the default package. -->
	<package name="default" extends="struts-default">
         ...
	</package>
</struts>

For now, the struts.xml just defines a default package (with the <package> section) where framework elements like actions, results and interceptors are registered.

(lightbulb) See also: struts.xml

Next

Onward to Hello World

Prev

Return to Ready, Set, Go!

  • No labels

4 Comments

  1. any plugin dependencies instead of dependenckies.

  2. Thanks guys.
    I just tried this (for 2.0.9)and it seems that org/springframework/context/ApplicationContextAware was required
    I only found this in spring.jar
    It worked when I added spring.jar and put in a spring listener into web.xml and a applicationContext.xml.

    I many well be wrong but I thought you would like to know if I happen to be right.
    Chad

  3. Hi guys – commons-fileupload.jar is also a required library as of Struts 2.1.2, so it belongs in WEB-INF/lib with the others. Source: http://www.manning-sandbox.com/thread.jspa?messageID=87065#80308

    As it turns out, I did not require spring.jar – maybe the previous poster was using something more complex than the tutorial project?

  4. Since commons-fileupload depends on commons-io, please add that too. Commons-fileupload dependencies: http://commons.apache.org/fileupload/dependencies.html.

    This is commons-fileupload FAQ:

    "Why is NoClassDefFoundError being thrown?

    There are two common causes for this error.

    Firstly, it might simply mean that you do not have the Commons IO jar in your classpath. FileUpload depends on IO (see dependencies) - you can tell if this is the case if the missing class is within the org.apache.commons.io package.

    Secondly this happens when attempting to rely on a shared copy of the Commons FileUpload jar file provided by your web container. The solution is to include the FileUpload jar file as part of your own web application, instead of relying on the container. The same may hold for FileUpload's IO dependency."

    Also, the filter-class should be org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.
    org.apache.struts2.dispatcher.FilterDispatcher is deprecated since 2.1.3 (question).