If for some reason the blank template or archetype doesn't work for you, here's how to out, we can just setup an application from square onescratch.
Setup the Web Application File Structure
Code Block |
---|
/tutorial/ /tutorial/META-INF/ /tutorial/WEB-INF/ /tutorial/WEB-INF/classes/struts.xml /tutorial/WEB-INF/lib/ /tutorial/WEB-INF/lib/CORE&OPTIONAL *.jarminimum JARs + any plugin JARs + plugin dependencies /tutorial/WEB-INF/web.xml |
- Copy to your
webapp/lib
directory- the
struts2-core-(VERSION).jar
, - all the
*.jar
files in/lib/default
, and - any necessary optional
*.jar
files from/lib/
. - required JARs (see next section),
- any Struts plugin JARs,
- any plugin dependencies.
- the
Tip |
---|
To customize the Struts |
Tip |
If you need to customize your own templates (how HTML is rendered from Struts UI the tags), copy into your the application's |
...
Filename | Description | ||
---|---|---|---|
| Framework library itself, found in distribution root directory | ||
| XWork 2 library on which Struts 2 is built (version 2.0 or later) | ||
| OSCore, a general-utility library from OpenSymphony | ||
| Object Graph Navigation Language (OGNL), the expression language used throughout the framework | ||
| Java bytecode manipulation library used by OGNL | ||
| All UI tag templates are written in Freemarker (also a good option for your own views) | ||
| 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 (also a good option for your own views) | ||
| The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications. | ||
| Commons IO is a library of utilities to assist with developing IO functionality. | ||
| Commons Lang3 is used to simplify usage of common tasks and code shortcuts to to stay DRY | | The default dependency injection container for the framework. |
| Java web application configuration file that defines the filters (and other components) for your web application | ||
| 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 tooIf 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 {{ Wiki Markup 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>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher<ng.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</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 container. The framework uses Spring internally. (You may wish to use Spring to deploy your own objects too.)
If other packages are being used, like SiteMesh or Spring, then other filters may need to be configured too
See also: web.xml
Setup the Struts Configuration (struts.xml
)
...
Code Block | ||
---|---|---|
| ||
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><!-- Include framework defaults (from Struts 2 JAR). --> <include file="struts-default.xml" /> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> ... </package> </struts> |
For now, the struts.xml
does only two things:
- It tells the framework that it should import the configuration information from
struts-default.xml
. (This file is located at the root of thestruts2-core.jar
, so it is sure to be found.)
...
just defines a default package (with the <package> section) where framework elements like actions
, results
and interceptors
are registered.
See also: struts.xml
Next | Onward to Hello World |
---|---|
Prev | Return to Bootstrap Ready, Set, Go! |