Source

webwork / docs / wwfreemarker.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>WebWork Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
</head>
<body>
  <div id="page-logo">
    <a href="index.html"><img src="logo-small.png" border="0"/></a>
  </div>
    <div class="snip-title">
	  <h1 class="snip-name">WebWorkFreemarkerSupport
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 these docs are a realllly quick hack together to get something up on the wiki.<p class="paragraph"/>I will try and polish them up over the next few days / weeks as time permits.<p class="paragraph"/>Please message the webwork mailing list for assistance.
<h3 class="heading-1">Configuring web.xml
</h3><p class="paragraph"/>To use freemarker as a view engine, the webwork2 FreemarkerServlet needs to be configured, and mapped to the file extension that you use for your templates.<p class="paragraph"/><div class="code"><pre>&#60;servlet&#62;
  &#60;servlet&#45;name&#62;freemarker&#60;/servlet&#45;name&#62;
  &#60;servlet&#45;class&#62;com.opensymphony.webwork.views.freemarker.FreemarkerServlet&#60;/servlet&#45;class&#62;
&#60;/servlet&#62;<p class="paragraph"/>&#60;servlet&#45;mapping&#62;
  &#60;servlet&#45;name&#62;freemarker&#60;/servlet&#45;name&#62;
  &#60;url&#45;pattern&#62;&#42;.ftl&#60;/url&#45;pattern&#62;
&#60;/servlet&#45;mapping&#62;</pre></div>
<h3 class="heading-1-1">xwork.xml configuration
</h3><p class="paragraph"/>To use the freemarker view, just use the DISPATCHER result type, and specify the location to the template file.<p class="paragraph"/><div class="code"><pre>&#60;action name=<span class="java&#45;quote">"test"</span> class=<span class="java&#45;quote">"<span class="java&#45;keyword">package</span>.Test"</span>&#62;
  &#60;result name=<span class="java&#45;quote">"success"</span> type=<span class="java&#45;quote">"dispatcher"</span>&#62;/WEB&#45;INF/views/testView.ftl&#60;/result&#62;
&#60;/action&#62;</pre></div>
<h3 class="heading-1-1">objects in the context
</h3>
<ul class="star">
<li>statck - the value stack for manually looking up expressions using stack.findValue(String).</li>
</ul><p class="paragraph"/>NOTE : your action properties are automatically resolved just like in a velocity view. so ${name} will result in stack.findValue("name"), which results in action.getName() being executed.
<ul class="star">
<li>wwUtil - an instance of the com.opensymphony.webwork.views.freemarker.FreemarkerUtil class.</li>
</ul><p class="paragraph"/>This class contains useful methods to execute OGNL expressions against arbitary objects, and a method to generate a select list using the &#60;ww:select&#62; pattern. (i.e. taking the name of the list property, a listKey and listValue)
<h3 class="heading-1-1">extending the servlet
</h3><p class="paragraph"/>Please refer to the freemarker site for details about the base freemarker servlet.<p class="paragraph"/>Be carfeul when subclassing com.opensymphony.webwork.views.freemarker.FreemarkerServlet when overriding<p class="paragraph"/><div class="code"><pre><span class="java&#45;keyword">protected</span> TemplateModel createModel(<p class="paragraph"/>    ObjectWrapper wrapper,<p class="paragraph"/>    ServletContext servletContext,<p class="paragraph"/>    HttpServletRequest request,<p class="paragraph"/>    HttpServletResponse response)</pre></div><p class="paragraph"/>Please call super.createModel(...) and wrap it with a new model to avoid problems with action property resoloution.
<h3 class="heading-1-1">using webwork UI tags
</h3><p class="paragraph"/>Freemarker has builtin support for using JSP taglibs. You can use JSP taglibs in FreeMarker even if
a) your servlet container has no support for JSP, or 
b) you didn't specify the taglib in your web.xml - note how in the example below we refer to the taglib by its webapp-absolute URL, so no configuration in web.xml is needed.<p class="paragraph"/><div class="code"><pre>&#60;#assign ww=JspTaglibs&#91;<span class="java&#45;quote">"/WEB&#45;INF/webwork.tld"</span>&#93; /&#62;<p class="paragraph"/>&#60;@ww.form method=<span class="java&#45;quote">"'post'"</span> name=<span class="java&#45;quote">"'inputform'"</span> action=<span class="java&#45;quote">"'save.action'"</span> &#62;<p class="paragraph"/>    &#60;@ww.hidden name=<span class="java&#45;quote">"'id'"</span> /&#62;<p class="paragraph"/>    &#60;@ww.textarea label=<span class="java&#45;quote">"'Details'"</span> name=<span class="java&#45;quote">"'details'"</span> rows=5 cols=40 /&#62;<p class="paragraph"/>    &#60;@ww.submit value=<span class="java&#45;quote">"'Save'"</span> align=<span class="java&#45;quote">"center"</span> /&#62;<p class="paragraph"/>&#60;/@ww.form&#62;</pre></div><p class="paragraph"/>NOTE : numeric properties for tags MUST be numbers, not strings.  as in the rows and cols properties above.  if  you use cols="40" you will receive an exception.  Other than that, the freemarker tag container behaves as you would expect.<p class="paragraph"/>
  </div>
</body>
</html>