Struts 2 > Lesson 1 - Setting up webwork in a web application |

Struts 2
Lesson 1 - Setting up webwork in a web application
Edit Page Browse Space Add Page Add News
Added by vitorsouza, last edited by Ted Husted on Sep 03, 2006 (view change) show comment hide comment
Comment:
Update parent page
View page history

# Lesson 1: Setting up webwork in a web application

For this lesson, you need to have a Servlet container set up and know how to create a web application. If you don't, we suggest you learn about Apache Tomcat, which is a free Servlet container from the Apache Jakarta Project, or Resin, from Caucho Technology, which is free for noncommercial use.

| Notation Throughout these lessons, we'll assume that your web application root is the directory [webapp], and that your Java source files are kept in [src].

To install WebWork in a web application:

1. Download WebWork. The current version can be found at WebWork's home page. This tutorial is based on version 2.1.7.
2. Set up an empty web application. For example, if you are using Tomcat, this will have something like the following directories (the directory "webwork-lessons" is referred to as [webapp] in these lessons):

       [the tomcat root directory]
       \|_webapps
         \|_webwork-lessons
           \|_WEB-INF
             \|_classes
             \|_lib

3. Copy the required WebWork libraries to your web application:

- copy `webwork-2.1.7.jar` to `[webapp]/WEB-INF/lib` ,
- copy `lib/core/*.jar` to `[webapp]/WEB-INF/lib` (not to `[webapp]/WEB-INF/lib/core`).

1. Configure `[webapp]/WEB-INF/web.xml`, and create `[webapp]/WEB-INF/classes/xwork.xml` and `[webapp]/WEB-INF/classes/validators.xml`, as described below.

   | WebWork jar name If you have a later version of WebWork than 2.1.7, the WebWork jar will not be named webwork-2.1.7.jar. Be sure to replace all occurrences of this jar's name below with the name of the jar you are using.

### web.xml:

Create the following `web.xml` file in `[webapp]/WEB-INF`. If you already have a `web.xml` file, just add the content of the <web-app> tag below to your existing <web-app> tag.

    <?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 WebWork Application</display-name>
    	<servlet>
    		<servlet-name>webwork</servlet-name>
    		<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>webwork</servlet-name>
    		<url-pattern>*.action</url-pattern>
    	</servlet-mapping>
    	<taglib>
    		<taglib-uri>webwork</taglib-uri>
    		<taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location>
    	</taglib>
    </web-app>

This registers `ServletDispatcher` as a servlet, and maps it to the suffix `*.action`. We will go into this more in the section on Actions in the next lesson. WebWork's taglib descriptor allows WebWork tags to be used (see lesson 4.1).

> Read more: web.xml

### xwork.xml:

Create the following file `xwork.xml` in `[webapp]/WEB-INF/classes/`.

    <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
    "http://www.opensymphony.com/xwork/xwork-1.0.dtd">

    <xwork>
    	<!-- Include webwork defaults (from WebWork JAR). -->
    	<include file="webwork-default.xml" />

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

For now, this xwork.xml does only two things:

- It informs WebWork that it should import the configuration information from `webwork-default.xml`. (This file is located at the root of the `webwork-2.1.7.jar`, so it is sure to be found.)

- It defines a default package (with the <package> section) where WebWork elements like actions, results and interceptors are registered.

  > Read more: struts.xml

  ### validators.xml:

Create a file `validators.xml` in `[webapp]/WEB-INF/classes/` with the following content:

    <validators>
    	<validator name="required"
    		class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
    	<validator name="requiredstring"
    		class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
    	<validator name="int"
    		class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
    	<validator name="date"
    		class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
    	<validator name="expression"
    		class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
    	<validator name="fieldexpression"
    		class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
    	<validator name="email"
    		class="com.opensymphony.xwork.validator.validators.EmailValidator"/>
    	<validator name="url"
    		class="com.opensymphony.xwork.validator.validators.URLValidator"/>
    	<validator name="visitor"
    		class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>
    	<validator name="conversion"
    		class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
    </validators>

This file defines the validators used, for example, for validating html form fields.

> Read more: Validation

## All Set Up!

Restart your servlet container (for example, restart Tomcat), and your webapp should be ready for use as a skeleton WebWork application.

To test whether everything is working, create `[webapp]/test.jsp`:

    <html>
    <body>
    Hello html world
    <hr/>
    <%
      out.println("Hello jsp world.");
    %>
    </body>
    </html>

If you can load this file in your browser and see the two Hello messages, your web application is working.

Next Lesson