Source

webwork / docs / wikidocs / TutorialLesson04-03.html

Full commit
<html>
    <head>
        <title>WebWork 2 : TutorialLesson04-03</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 : TutorialLesson04-03
                                                    </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="TutorialLesson04-03-Lesson4.3%3AUsingFreemarkerwithWebWork"> Lesson 4.3: Using Freemarker with WebWork</a></h1></p>Freemarker is a powerfull template engine that competes with Velocity. You can learn more about it in the project&#039;s homepage: <a href="http://freemarker.sourceforge.net" title="Visit page outside Confluence">&#104;ttp://freemarker.sourceforge.net</a>.<p class="paragraph">First of all, to use Freemarker with Webwork, you have to place the freemarker.jar in your <tt class="monospaced">WEB-INF\lib</tt> folder. You can download the distribution <a href="http://freemarker.sourceforge.net/freemarkerdownload.html" title="Visit page outside Confluence">here</a>.</p>After that, just configure <tt class="monospaced">web.xml</tt> and start writing your templates, as explained below.
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson04-03-web.xml%3A"> web.xml:</a></h3></p>To use Freemarker as the view, you need to modify <tt class="monospaced">web.xml</tt> and add a servlet and a servlet mapping for <tt class="monospaced">FreemarkerServlet</tt>, as demonstrated below:<br/>

<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;servlet&gt;</span>&#10;	<span class="xml&#45;tag">&lt;servlet&#45;name&gt;</span>freemarker<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.views.freemarker.FreemarkerServlet<span class="xml&#45;tag">&lt;/servlet&#45;class&gt;</span>&#10;		<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; FreemarkerServlet settings: &#45;&#45;&gt;</span></span>&#10;		<span class="xml&#45;tag">&lt;init&#45;param&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;name&gt;</span>TemplatePath<span class="xml&#45;tag">&lt;/param&#45;name&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;value&gt;</span>/<span class="xml&#45;tag">&lt;/param&#45;value&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/init&#45;param&gt;</span>&#10;		<span class="xml&#45;tag">&lt;init&#45;param&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;name&gt;</span>NoCache<span class="xml&#45;tag">&lt;/param&#45;name&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;value&gt;</span>true<span class="xml&#45;tag">&lt;/param&#45;value&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/init&#45;param&gt;</span>&#10;		<span class="xml&#45;tag">&lt;init&#45;param&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;name&gt;</span>ContentType<span class="xml&#45;tag">&lt;/param&#45;name&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;value&gt;</span>text/html<span class="xml&#45;tag">&lt;/param&#45;value&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/init&#45;param&gt;</span>&#10;		<span class="xml&#45;tag">&lt;init&#45;param&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;name&gt;</span>default&#95;encoding<span class="xml&#45;tag">&lt;/param&#45;name&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;value&gt;</span>ISO&#45;8859&#45;1<span class="xml&#45;tag">&lt;/param&#45;value&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/init&#45;param&gt;</span>&#10;		<span class="xml&#45;tag">&lt;init&#45;param&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;name&gt;</span>number&#95;format<span class="xml&#45;tag">&lt;/param&#45;name&gt;</span>&#10;			<span class="xml&#45;tag">&lt;param&#45;value&gt;</span>0.##########<span class="xml&#45;tag">&lt;/param&#45;value&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/init&#45;param&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;&#10;<span class="xml&#45;tag">&lt;servlet&#45;mapping&gt;</span> &#10;	<span class="xml&#45;tag">&lt;servlet&#45;name&gt;</span>freemarker<span class="xml&#45;tag">&lt;/servlet&#45;name&gt;</span> &#10;	<span class="xml&#45;tag">&lt;url&#45;pattern&gt;</span>&#42;.ftl<span class="xml&#45;tag">&lt;/url&#45;pattern&gt;</span> &#10;<span class="xml&#45;tag">&lt;/servlet&#45;mapping&gt;</span></pre>
</div></div><p class="paragraph">The configuration above means that Freemarker templates can be rendered through requests to <tt class="monospaced">.ftl</tt> pages. That also means that you should implement security checks in your templates so an user doesn&#039;t access it directly without going through an action first (if that is required). But you can always place your Freemarker files under WEB-INF so they become unaccessible to direct requests. We will use the latter approach in our examples.</p>Inside a Freemarker template, you will have access to every object managed by WebWork with the following syntax:<br/>

<ul class="star">
<li> <b class="strong"><tt class="monospaced">$stack</tt></b> = <tt class="monospaced">OgnlValueStack</tt>;</li>
<li> <b class="strong"><tt class="monospaced">$webwork</tt></b> = <tt class="monospaced">FreemarkerWebWorkUtil</tt>, a toolbox providing services like formatting url, accessing the value stack, etc;</li>
<li> <b class="strong"><tt class="monospaced">$name-of-property</tt></b> = property retrieved from the value stack. If that fails, it looks up an attribute with that name in the <tt class="monospaced">HttpServletRequest</tt>, <tt class="monospaced">HttpSession</tt> and <tt class="monospaced">ServletContext</tt>, in that order;</li>
<li> <b class="strong"><tt class="monospaced">$Request</tt></b> = <tt class="monospaced">HttpServletRequest</tt>;</li>
<li> <b class="strong"><tt class="monospaced">$Session</tt></b> = <tt class="monospaced">HttpServletResponse</tt>;</li>
<li> <b class="strong"><tt class="monospaced">$Application</tt></b> = <tt class="monospaced">OgnlValueStack</tt>.</li>
</ul><br/>
The example below does the same thing as example 2 from <a href="TutorialLesson03.html" title="TutorialLesson03">lesson 3</a>, but now, using Freemarker templates.
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson04-03-xwork.xml%3A"> xwork.xml:</a></h3>
<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"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Default interceptor stack. &#45;&#45;&gt;</span></span> &#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"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Action: Lesson 4.3: HelloAction. &#45;&#45;&gt;</span></span>&#10;		<span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;indexFreemarker&quot;</span> class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.ActionSupport&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>/WEB&#45;INF/ftl/lesson3/index.ftl<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/action&gt;</span>&#10;&#10;		<span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;helloFreemarker&quot;</span> class=<span class="xml&#45;quote">&quot;lesson03.HelloAction&quot;</span>&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;error&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>/WEB&#45;INF/ftl/lesson3/index.ftl<span class="xml&#45;tag">&lt;/result&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>/WEB&#45;INF/ftl/lesson3/success.ftl<span class="xml&#45;tag">&lt;/result&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/action&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><h3 class="heading3"><a name="TutorialLesson04-03-HelloAction.java%28sameaslesson3%29%3A"> HelloAction.java (same as lesson 3):</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">package</span> lesson03;&#10;&#10;<span class="java&#45;keyword">import</span> com.opensymphony.xwork.ActionSupport;&#10;&#10;<span class="java&#45;keyword">public</span> class HelloAction <span class="java&#45;keyword">extends</span> ActionSupport &#123;&#10;	<span class="java&#45;object">String</span> person;&#10;	<span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">String</span> getPerson() &#123;&#10;		<span class="java&#45;keyword">return</span> person;&#10;	&#125;&#10;	<span class="java&#45;keyword">public</span> void setPerson(<span class="java&#45;object">String</span> person) &#123;&#10;		<span class="java&#45;keyword">this</span>.person = person;&#10;	&#125;&#10;	<span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">String</span> execute() <span class="java&#45;keyword">throws</span> Exception &#123;&#10;		<span class="java&#45;keyword">if</span> ((person == <span class="java&#45;keyword">null</span>) || (person.length() == 0)) <span class="java&#45;keyword">return</span> ERROR;&#10;		<span class="java&#45;keyword">else</span>&#160;<span class="java&#45;keyword">return</span> SUCCESS;&#10;	&#125;&#10;&#125;</pre>
</div></div>
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson04-03-ex02index.ftl"> ex02-index.ftl</a></h3>
<div class="code"><div class="codeContent">
<pre>&lt;#assign ww=JspTaglibs&#91;<span class="java&#45;quote">&quot;/WEB&#45;INF/lib/webwork.tld&quot;</span>&#93; /&gt;&#10;&#10;&lt;html&gt;&#10;&lt;head&gt;&#10;&lt;title&gt;WebWork Tutorial &#45; Lesson 4.3 &#45; Example 1&lt;/title&gt;&#10;&lt;/head&gt;&#10;&#10;&lt;body&gt;&#10;&#10;&lt;p&gt;Click &lt;a href=<span class="java&#45;quote">&quot;$&#123;wwUtil.buildUrl(&#039;indexFreemarker.action&#039;)&#125;&quot;</span>&gt;here&lt;/a&gt; to reload <span class="java&#45;keyword">this</span> page.&lt;/p&gt;&#10;&#10;&lt;&#64;ww.form name=<span class="java&#45;quote">&quot;&#039;nameForm&#039;&quot;</span> action=<span class="java&#45;quote">&quot;&#039;helloFreemarker.action&#039;&quot;</span> method=<span class="java&#45;quote">&quot;&#039;POST&#039;&quot;</span>&gt;&#10;	&lt;&#64;ww.textfield label=<span class="java&#45;quote">&quot;&#039;What is your name &#63;&#039;&quot;</span> name=<span class="java&#45;quote">&quot;&#039;person&#039;&quot;</span> value=<span class="java&#45;quote">&quot;person&quot;</span> size=<span class="java&#45;quote">&quot;20&quot;</span>/&gt;&#10;	&lt;&#64;ww.submit name=<span class="java&#45;quote">&quot;&#039;submit&#039;&quot;</span> value=<span class="java&#45;quote">&quot;&#039;Submit&#039;&quot;</span>/&gt;&#10;&lt;/&#64;ww.form&gt;&#10;&#10;&lt;/body&gt;&#10;&lt;/html&gt;</pre>
</div></div></p>If you don&#039;t want to use WebWork&#039;s UI Tags, you could do it like this:<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson04-03-ex02indexnotags.ftl"> ex02-index-notags.ftl</a></h3>
<div class="code"><div class="codeContent">
<pre>&lt;html&gt;&#10;&lt;head&gt;&#10;&lt;title&gt;WebWork Tutorial &#45; Lesson 4.3 &#45; Example 1&lt;/title&gt;&#10;&lt;/head&gt;&#10;&#10;&lt;body&gt;&#10;&#10;&lt;p&gt;Click &lt;a href=<span class="java&#45;quote">&quot;$&#123;wwUtil.buildUrl(&#039;indexFreemarker.action&#039;)&#125;&quot;</span>&gt;here&lt;/a&gt; to reload <span class="java&#45;keyword">this</span> page.&lt;/p&gt;&#10;&#10;&lt;form name=<span class="java&#45;quote">&quot;nameForm&quot;</span> action=<span class="java&#45;quote">&quot;$&#123;wwUtil.buildUrl(&#039;helloFreemarker.action&#039;)&#125;&quot;</span> method=<span class="java&#45;quote">&quot;POST&quot;</span>&gt;&#10;	What is your name &#63; &#10;	&lt;input type=<span class="java&#45;quote">&quot;text&quot;</span> name=<span class="java&#45;quote">&quot;person&quot;</span> value=<span class="java&#45;quote">&quot;$&#123;person&#125;&quot;</span> size=<span class="java&#45;quote">&quot;20&quot;</span>&gt;&#10;	&lt;input type=<span class="java&#45;quote">&quot;submit&quot;</span> name=<span class="java&#45;quote">&quot;submit&quot;</span> value=<span class="java&#45;quote">&quot;Submit&quot;</span>&gt;&#10;&lt;/form&gt;&#10;&lt;/body&gt;&#10;&lt;/html&gt;</pre>
</div></div></p>However, if you choose no to use tags, it&#039;s recommended that you use Freemarker Macros to write the form elements.
<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson04-03-ex02success.ftl%3A"> ex02-success.ftl:</a></h3>
<div class="code"><div class="codeContent">
<pre>&lt;#assign ww=JspTaglibs&#91;<span class="java&#45;quote">&quot;/WEB&#45;INF/lib/webwork.tld&quot;</span>&#93; /&gt;&#10;&#10;&lt;html&gt; &#10;&lt;head&gt; &#10;&lt;title&gt;WebWork Tutorial &#45; Lesson 4.3 &#45; Example 1&lt;/title&gt; &#10;&lt;/head&gt; &#10;&lt;body&gt; &#10;&#10;Come from the property WW tag (taglibs support) : &lt;&#64;ww.property value=<span class="java&#45;quote">&quot;person&quot;</span>/&gt; &lt;br&gt;&#10;Come from the Freemarker lookup in the WW stack : $&#123;person&#125;&#10;&#10;&lt;/body&gt; &#10;&lt;/html&gt;</pre>
</div></div></p>You can use either WebWork <tt class="monospaced">property</tt> tag or the Freemarker <tt class="monospaced">$person</tt> reference. Both of them return the same thing: a property from the action class.<p class="paragraph"><hr class="line"/></p><a href="TutorialLesson04-02.html" title="TutorialLesson04-02">Previous Lesson</a> | <a href="TutorialLesson05.html" title="TutorialLesson05">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 18, 2004 00:08</font></td>
		    </tr>
	    </table>
    </body>
</html>