webwork / docs / wikidocs / xwork.xml.html

<html>
    <head>
        <title>WebWork 2 : xwork.xml</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 : xwork.xml
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on May 26, 2004 by <font color="#0050B2">ctran</font>.
				    </div>

				    <p class="paragraph"><h2 style="margin: 4px 0px 4px 0px;" class="heading2"><a name="xwork.xml-Example"> Example</a></h2></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> encoding=<span class="xml&#45;quote">&quot;ISO&#45;8859&#45;1&quot;</span>&#63;&gt;</span>&#10;<span class="xml&#45;tag">&lt;&#33;DOCTYPE xwork &#10;          PUBLIC &#10;          <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;<span class="xml&#45;tag">&lt;xwork&gt;</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">&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;&#10;        <span class="xml&#45;tag">&lt;interceptors&gt;</span>&#10;            <span class="xml&#45;tag">&lt;interceptor&#45;stack name=<span class="xml&#45;quote">&quot;defaultComponentStack&quot;</span>&gt;</span>&#10;                <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;component&quot;</span>/&gt;</span>&#10;                <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>/&gt;</span>&#10;            <span class="xml&#45;tag">&lt;/interceptor&#45;stack&gt;</span>&#10;        <span class="xml&#45;tag">&lt;/interceptors&gt;</span>&#10;&#10;        <span class="xml&#45;tag">&lt;default&#45;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>/&gt;</span>&#10;&#10;        <span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;SimpleCounter&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.example.counter.SimpleCounter&quot;</span>&gt;</span>&#10;            <span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>/success.jsp<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;            <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultComponentStack&quot;</span>/&gt;</span>&#10;        <span class="xml&#45;tag">&lt;/action&gt;</span>&#10;&#10;        <span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45;&#10;          &#45; Velocity implementation of the SimpleCounter.  Also demonstrate a more verbose version of result element&#10;          &#45;&#45;&gt;</span></span>&#10;        <span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;VelocityCounter&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.example.counter.SimpleCounter&quot;</span>&gt;</span>&#10;            <span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;velocity&quot;</span>&gt;</span>&#10;                <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;location&quot;</span>&gt;</span>/success.vm<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;            <span class="xml&#45;tag">&lt;/result&gt;</span>&#10;            <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultComponentStack&quot;</span>/&gt;</span>&#10;        <span class="xml&#45;tag">&lt;/action&gt;</span>&#10;&#10;        <span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45;&#10;            &#45; Different method can be used (processForm). &#10;            &#45;&#45;&gt;</span></span>&#10;        <span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;formTest&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.example.FormAction&quot;</span> method=<span class="xml&#45;quote">&quot;processForm&quot;</span> &gt;</span>&#10;            <span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>/formSuccess.jsp<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;            <span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;invalid.token&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>/form.jsp<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;            <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>/&gt;</span>&#10;            <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;token&quot;</span>/&gt;</span>&#10;        <span class="xml&#45;tag">&lt;/action&gt;</span>&#10;&#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"><h2 class="heading2"><a name="xwork.xml-Actions"> Actions</a></h2></p><div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;formTest&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.example.FormAction&quot;</span> method=<span class="xml&#45;quote">&quot;processForm&quot;</span>&gt;</span></pre>
</div></div><p class="paragraph">Actions are the basic &quot;unit-of-work&quot; in WebWork, they define, well, actions. An action will usually be a request, (and usually a button click, or form submit). The main action element (tag is too synonymous with JSP) has two parts, the friendly name (referenced in the URL, i.e. saveForm.action) and the corresponding &quot;handler&quot; class.</p>The optional &quot;<b class="strong">method</b>&quot; parameter tells WebWork which method to call based upon this action. If you leave the method parameter blank, WebWork will call the method <b class="strong">execute()</b> by default. If there is no execute() method and no method specified in the xml file, WebWork will throw an exception.   <p class="paragraph">Also, you can tell WebWork to invoke &quot;<b class="strong">doSomething</b>&quot;  method in your action by using the pattern &quot;<b class="strong">actionName!something</b>&quot; in your form.  For example, &quot;<b class="strong">formTest!save.action</b>&quot; will invoke the method &quot;<b class="strong">save</b>&quot; in FormAction class.  The method must be public and take no arguments:</p><div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">String</span> save() <span class="java&#45;keyword">throws</span> Exception&#10;  &#123;&#10;      ...&#10;      <span class="java&#45;keyword">return</span> SUCCESS;&#10;  &#125;</pre>
</div></div><p class="paragraph">All the configuration for &quot;<b class="strong">actionName</b>&quot; will be used for &quot;actionName!something&quot; (interceptors, result types, etc...)</p><h2 class="heading2"><a name="xwork.xml-Results"> Results</a></h2>
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;missing&#45;data&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;location&quot;</span>&gt;</span>/form.jsp<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;parameterA&quot;</span>&gt;</span>A<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;parameterB&quot;</span>&gt;</span>B<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;<span class="xml&#45;tag">&lt;/result&gt;</span></pre>
</div></div><p class="paragraph">Result tags tell WebWork what to do next after the action has been called.   The &quot;name&quot; attribute maps to the result code returned from the action execute() method.  The &quot;type&quot; attribute indicates what result type class to use (see <a href="Result Types.html" title="Result Types">Result Types</a>).  The &quot;param&quot; elements allow you to pass parameters to<br/>
the view:
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;result&#45;types&gt;</span>&#10;     ....&#10;    <span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;header&quot;</span>                    &#10;         class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.dispatcher.HttpHeaderResult&quot;</span>/&gt;</span>&#10;		&#10;<span class="xml&#45;tag">&lt;/result&#45;types&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;no&#45;content&quot;</span> type=<span class="xml&#45;quote">&quot;header&quot;</span>&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;status&quot;</span>&gt;</span>204<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;headers.customHeaderA&quot;</span>&gt;</span>A<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;    <span class="xml&#45;tag">&lt;param name=<span class="xml&#45;quote">&quot;headers.customHeaderB&quot;</span>&gt;</span>B<span class="xml&#45;tag">&lt;/param&gt;</span>&#10;<span class="xml&#45;tag">&lt;/result&gt;</span></pre>
</div></div></p>There are a standard set of result codes built-in to WebWork, (in the Action interface) they include: 
<div class="code"><div class="codeContent">
<pre>Action.SUCCESS = <span class="java&#45;quote">&quot;success&quot;</span>;&#10;Action.NONE    = <span class="java&#45;quote">&quot;none&quot;</span>;&#10;Action.ERROR   = <span class="java&#45;quote">&quot;error&quot;</span>;&#10;Action.INPUT   = <span class="java&#45;quote">&quot;input&quot;</span>;&#10;Action.LOGIN   = <span class="java&#45;quote">&quot;login&quot;</span>;</pre>
</div></div><p class="paragraph">You can extend these result codes as you see fit (i.e &quot;missing-data&quot;). Most of the time you will have either SUCCESS or ERROR, with SUCCESS moving on to the next page in your application.</p>If you only need to specify the &quot;location&quot; parameter, you can use the short form:
<div class="code"><div class="codeContent">
<pre>&lt;result name=<span class="java&#45;quote">&quot;missing&#45;data&quot;</span> type=<span class="java&#45;quote">&quot;dispatcher&quot;</span>&gt;/form.jsp&lt;/result&gt;</pre>
</div></div><p class="paragraph">See <a href="webwork-default.xml.html" title="webwork-default.xml">webwork-default.xml</a> or <a href="Result Types.html" title="Result Types">Result Types</a> for standard result types.</p><h2 class="heading2"><a name="xwork.xml-Interceptors"> Interceptors</a></h2><p class="paragraph">Interceptors allow you to define code to be executed before and/or after the execution of an action. Interceptors can be a powerful tool when writing web applications. Some of the most common implementations of an Interceptor might be: 
<ul class="star">
<li> Security Checking (ensuring the user is logged in)</li>
<li> Trace Logging (logging every action)</li>
<li> Bottleneck Checking (start a timer before and after every action, to check bottlenecks in your application)<br/>
You can also chain Interceptors together to create an interceptor <b class="strong">stack</b>. If you wanted to do a login check, security check, and logging all before an Action call, this could easily be done with an interceptor package.
</li>
</ul><br/>
Intercepters must first be defined (to give name them) and can be chained together as a stack:</p><div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;interceptors&gt;</span>&#10;  <span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;security&quot;</span> class=<span class="xml&#45;quote">&quot;com.mycompany.security.SecurityInterceptor&quot;</span>/&gt;</span>&#10;  <span class="xml&#45;tag">&lt;interceptor&#45;stack name=<span class="xml&#45;quote">&quot;defaultComponentStack&quot;</span>&gt;</span>&#10;    <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;component&quot;</span>/&gt;</span>&#10;    <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>/&gt;</span>&#10;  <span class="xml&#45;tag">&lt;/interceptor&#45;stack&gt;</span>&#10;<span class="xml&#45;tag">&lt;/interceptors&gt;</span></pre>
</div></div><p class="paragraph">To use them in your actions:
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;VelocityCounter&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.example.counter.SimpleCounter&quot;</span>&gt;</span>&#10;   <span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span>&gt;</span>...<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;   <span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultComponentStack&quot;</span>/&gt;</span>&#10;<span class="xml&#45;tag">&lt;/action&gt;</span></pre>
</div></div></p>NOTE: Reference name can be either the name of the interceptor or the name of a stack<p class="paragraph">For more details, see <a href="Interceptors.html" title="Interceptors">Interceptors</a> reference.</p><h2 class="heading2"><a name="xwork.xml-Views"> Views</a></h2><p class="paragraph">WebWork supports JSP and Velocity for your application presentation layer. For this example we will use a JSP file. Webwork comes packaged with a tag library (taglibs). You can use these taglibs as components in your JSP file. Here is an section of our form.jsp page:</p><div class="code"><div class="codeContent">
<pre>&lt;&#37;&#64; taglib prefix=<span class="java&#45;quote">&quot;ww&quot;</span> uri=<span class="java&#45;quote">&quot;webwork&quot;</span> &#37;&gt;&#10;&lt;html&gt;&#10;&lt;head&gt;&lt;title&gt;Webwork Form Example&lt;/title&gt;&lt;/head&gt;&#10;&lt;body&gt;&#10;   &lt;ww:form name=<span class="java&#45;quote">&quot;myForm&quot;</span> action=<span class="java&#45;quote">&quot;&#039;formTest&#039;&quot;</span> namespace=<span class="java&#45;quote">&quot;/&quot;</span> method=<span class="java&#45;quote">&quot;POST&quot;</span>&gt;&#10;  &lt;table&gt;&#10;    &lt;ww:textfield label=<span class="java&#45;quote">&quot;First Name&quot;</span> name=<span class="java&#45;quote">&quot;&#039;formBean.firstName&#039;&quot;</span> value=<span class="java&#45;quote">&quot;formBean.firstName&quot;</span>/&gt;&#10;    &lt;ww:textfield label=<span class="java&#45;quote">&quot;Last Name&quot;</span> name=<span class="java&#45;quote">&quot;&#039;formBean.lastName&#039;&quot;</span> value=<span class="java&#45;quote">&quot;formBean.lastName&quot;</span>/&gt;&#10;    &lt;ww:submit value=<span class="java&#45;quote">&quot;Save Form&quot;</span>/&gt;&#10;  &lt;/table&gt;&#10;&lt;/ww:form&gt;&#10;&lt;/body&gt;</pre>
</div></div><p class="paragraph">The process of events will go as follows:</p><ol>
<li> WebWork will take notice since the URI ends in <b class="strong">.action</b> (defined in <b class="strong">web.xml</b> files)</li>
<li> WebWork will look up the action <b class="strong">formTest</b> in its action hierarchy and call any Interceptors that might have been defined.</li>
<li> WebWork will translate <b class="strong">formTest</b> and decide to call the method <b class="strong">processForm</b> in the class <b class="strong">com.opensymphony.webwork.example.FormAction</b> as defined in <b class="strong">xwork.xml</b> file.</li>
<li> The method will process successfully and give WebWork the <b class="strong">SUCCESS</b> return parameter.</li>
<li> WebWork will translate the <b class="strong">SUCCESS</b> return parameter into the location <b class="strong">formSuccess.jsp</b> (as defined in <b class="strong">xwork.xml</b>) and redirect accordingly.</li>
</ol>
<h2 class="heading2"><a name="xwork.xml-Include"> Include</a></h2><p class="paragraph">To make it easy to manage large scale development (lots of actions +  configuration), WebWork allows you to include other configuration files from xwork.xml :</p><div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;xwork&gt;</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;    <span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;user.xml&quot;</span>/&gt;</span>&#10;    <span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;shoppingcart.xml&quot;</span>/&gt;</span>&#10;    <span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;product.xml&quot;</span>/&gt;</span>&#10;    ....&#10;<span class="xml&#45;tag">&lt;/xwork&gt;</span></pre>
</div></div><p class="paragraph">The included files must be the same format as xwork.xml (with the doctype and everything) and be placed on classpath (usually in /WEB-INF/classes or jar files in /WEB-INF/lib).</p>Most of the content here provided by Matt Dowell &lt;matt.dowell@notiva.com&gt;

				    
                    			    </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:03</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.