webwork / docs / wikidocs / TutorialLesson02.html

<html>
    <head>
        <title>WebWork 2 : TutorialLesson02</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            WebWork 2 : TutorialLesson02
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Sep 16, 2004 by <font color="#0050B2">vitorsouza</font>.
				    </div>

				    <p class="paragraph"><h1 style="margin: 4px 0px 4px 0px;" class="heading1"><a name="TutorialLesson02-Lesson2%3ASettinguptheWebApplication"> Lesson 2: Setting up the Web Application</a></h1></p>It is assumed that you have a Servlet container set up and you know how to create a web application. If you don&#039;t, we suggest you learn about  <a href="http://jakarta.apache.org/tomcat/" title="Visit page outside Confluence">Apache Tomcat</a>, which is a free Servlet container from the Apache Jakarta Project, or Resin, from <a href="http://www.caucho.com/" title="Visit page outside Confluence">Caucho Technology</a>, which is free for non-comercial use.<p class="paragraph">To use WebWork, copy the required libraries, described in the previous lesson (<tt class="monospaced">webwork-2.1.1.jar</tt> and <tt class="monospaced">lib/core/&#42;.jar</tt>), to the directory <tt class="monospaced">WEB-INF/lib</tt> of the Web Application. Then, configure <tt class="monospaced">web.xml</tt> and create other two XML files: <tt class="monospaced">xwork.xml</tt> and <tt class="monospaced">validators.xml</tt>. These three files will be explained below.</p><h3 class="heading3"><a name="TutorialLesson02-web.xml%3A"> web.xml:</a></h3><p class="paragraph">Change your web application&#039;s web.xml file to look somewhat like this:</p><div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#63;xml version=<span class="xml&#45;quote">&quot;1.0&quot;</span>&#63;&gt;</span>&#10;<span class="xml&#45;tag">&lt;&#33;DOCTYPE web&#45;app PUBLIC <span class="xml&#45;quote">&quot;&#45;//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;</span>&#10;<span class="xml&#45;quote">&quot;&#104;ttp://java.sun.com/dtd/web&#45;app&#95;2&#95;3.dtd&quot;</span>&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;web&#45;app&gt;</span>&#10;	<span class="xml&#45;tag">&lt;display&#45;name&gt;</span>My WebWork Application<span class="xml&#45;tag">&lt;/display&#45;name&gt;</span>&#10;	<span class="xml&#45;tag">&lt;servlet&gt;</span>&#10;		<span class="xml&#45;tag">&lt;servlet&#45;name&gt;</span>webwork<span class="xml&#45;tag">&lt;/servlet&#45;name&gt;</span>&#10;		<span class="xml&#45;tag">&lt;servlet&#45;class&gt;</span>com.opensymphony.webwork.dispatcher.ServletDispatcher<span class="xml&#45;tag">&lt;/servlet&#45;class&gt;</span>&#10;		<span class="xml&#45;tag">&lt;load&#45;on&#45;startup&gt;</span>1<span class="xml&#45;tag">&lt;/load&#45;on&#45;startup&gt;</span>&#10;	<span class="xml&#45;tag">&lt;/servlet&gt;</span>&#10;	<span class="xml&#45;tag">&lt;servlet&#45;mapping&gt;</span>&#10;		<span class="xml&#45;tag">&lt;servlet&#45;name&gt;</span>webwork<span class="xml&#45;tag">&lt;/servlet&#45;name&gt;</span>&#10;		<span class="xml&#45;tag">&lt;url&#45;pattern&gt;</span>&#42;.action<span class="xml&#45;tag">&lt;/url&#45;pattern&gt;</span>&#10;	<span class="xml&#45;tag">&lt;/servlet&#45;mapping&gt;</span>&#10;	<span class="xml&#45;tag">&lt;taglib&gt;</span>&#10;		<span class="xml&#45;tag">&lt;taglib&#45;uri&gt;</span>webwork<span class="xml&#45;tag">&lt;/taglib&#45;uri&gt;</span>&#10;		<span class="xml&#45;tag">&lt;taglib&#45;location&gt;</span>/WEB&#45;INF/lib/webwork&#45;2.1.1.jar<span class="xml&#45;tag">&lt;/taglib&#45;location&gt;</span>&#10;	<span class="xml&#45;tag">&lt;/taglib&gt;</span>&#10;<span class="xml&#45;tag">&lt;/web&#45;app&gt;</span></pre>
</div></div><p class="paragraph">To use WebWork you must register <tt class="monospaced">ServletDispatcher</tt> and its mapping to <tt class="monospaced">&#42;.action</tt>. It will be explained why those lines are important in the section about Actions in the <a href="TutorialLesson03.html" title="TutorialLesson03">next lesson</a>. In the example above WebWork&#039;s taglib descriptor is also declared to allow the usage of WebWork tags (more about WebWork tags on <a href="TutorialLesson04-01.html" title="TutorialLesson04-01">lesson 4.1</a>) &#8211; it is safe to remove it if WebWork tags are not being used.</p><blockquote class="blockquote"> Read more: <a href="web.xml.html" title="web.xml">web.xml</a></blockquote>
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson02-xwork.xml%3A"> xwork.xml:</a></h3></p>At the root of the classpath (namely, <tt class="monospaced">WEB-INF/classes</tt>) create a file called <tt class="monospaced">xwork.xml</tt>, which WebWork uses to configure itself. For now, place the following content in it:<br/>

<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#33;DOCTYPE xwork PUBLIC <span class="xml&#45;quote">&quot;&#45;//OpenSymphony Group//XWork 1.0//EN&quot;</span> &#10;<span class="xml&#45;quote">&quot;&#104;ttp://www.opensymphony.com/xwork/xwork&#45;1.0.dtd&quot;</span>&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;xwork&gt;</span>&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Include webwork defaults (from WebWork JAR). &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;webwork&#45;default.xml&quot;</span> /&gt;</span>&#10;	&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Configuration for the default package. &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;package name=<span class="xml&#45;quote">&quot;default&quot;</span> extends=<span class="xml&#45;quote">&quot;webwork&#45;default&quot;</span>&gt;</span>&#10;	<span class="xml&#45;tag">&lt;/package&gt;</span>&#10;<span class="xml&#45;tag">&lt;/xwork&gt;</span></pre>
</div></div><p class="paragraph">This is just a skeleton of a configuration file, which will be incremented as we go through the lessons. As of now, it is only doing two things:</p><ul class="star">
<li> informing WebWork that it should import the configuration information from <tt class="monospaced">webwork-default.xml</tt> (which is located at the root of <tt class="monospaced">webwork-2.1.1.jar</tt> and thus available for use) &#8211; this file defines the <tt class="monospaced">webwork-default</tt> package, which contains the <em class="emphasis">default</em> configuration for WebWork applications;</li>
</ul>
<ul class="star">
<li> defining a <tt class="monospaced">default</tt> package, which is where the actions, results and interceptors are registered. This package extends <tt class="monospaced">webwork-default</tt>, i.e., the <tt class="monospaced">default</tt> package will inherit all the configuration defined in <tt class="monospaced">webwork-default</tt>.</li>
</ul><br/>
<blockquote class="blockquote"> Read more: <a href="xwork.xml.html" title="xwork.xml">xwork.xml</a></blockquote>
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson02-validators.xml%3A"> validators.xml:</a></h3></p>Again, at the root of the classpath, create a file called <tt class="monospaced">validators.xml</tt>, with the following content:<br/>

<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#33;DOCTYPE validators PUBLIC <span class="xml&#45;quote">&quot;&#45;//OpenSymphony Group//XWork Validator&#10;1.0//EN&quot;</span>&#160;<span class="xml&#45;quote">&quot;&#104;ttp://www.opensymphony.com/xwork/xwork&#45;validator&#45;1.0.dtd&quot;</span>&gt;</span> &#10;&#10;<span class="xml&#45;tag">&lt;validators&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;required&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.RequiredFieldValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;requiredstring&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.RequiredStringValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;int&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.IntRangeFieldValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;date&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.DateRangeFieldValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;expression&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.ExpressionValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;fieldexpression&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.FieldExpressionValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;email&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.EmailValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;url&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.URLValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;visitor&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.VisitorFieldValidator&quot;</span>/&gt;</span> &#10;	<span class="xml&#45;tag">&lt;validator name=<span class="xml&#45;quote">&quot;conversion&quot;</span>&#10;		class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator&quot;</span>/&gt;</span> &#10;<span class="xml&#45;tag">&lt;/validators&gt;</span></pre>
</div></div><p class="paragraph">This file defines the validators that are available for use.</p><blockquote class="blockquote"> Read more: <a href="Validation.html" title="Validation">Validation</a></blockquote>
<p class="paragraph"><h2 class="heading2"><a name="TutorialLesson02-AllSetUp%21"> All Set Up!</a></h2></p>Now there is a skeleton of the WebWork application already set up. Next lessons will teach how to use WebWork&#039;s actions, views and interceptors.<p class="paragraph"><hr class="line"/></p><a href="TutorialLesson01.html" title="TutorialLesson01">Previous Lesson</a> | <a href="TutorialLesson03.html" title="TutorialLesson03">Next Lesson</a><br/>



				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Oct 15, 2004 02:04</font></td>
		    </tr>
	    </table>
    </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.