Source

webwork / docs / tagfromvelocity.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">Using WebWork 2 Tags from Velocity
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 <h3 class="heading-1">Using the VelocityContext
</h3><p class="paragraph"/>WebWork2 contains the following items by default in the ValueStack:
<ul class="star">
<li>req - the current HttpServletRequest</li>
<li>res - the current HttpServletResponse</li>
<li>stack - the current OgnlValueStack</li>
<li>ognl - an instance of OgnlTool</li>
<li>ui - a (now deprecated) instance of a ui tag renderer</li>
</ul><p class="paragraph"/>Additionally, WebWork uses a customized VelocityContext that allows properties from the value stack to be directly accessed.  This means that if you have the following Action:<p class="paragraph"/><div class="code"><pre><span class="java&#45;keyword">public</span> class SampleAction <span class="java&#45;keyword">implements</span> Action &#123;
  ...<p class="paragraph"/>  <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> getName() &#123; &#8230; &#125;
  <span class="java&#45;keyword">public</span> Address getAddress() &#123; &#8230; &#125;
&#125;</pre></div><p class="paragraph"/>You can, from Velocity, access name and address as $name and $address respectively.
<h3 class="heading-1">Examples
</h3><p class="paragraph"/>Say we have the following ui tags being rendered in a JSP page:<p class="paragraph"/><div class="code"><pre>&#60;ui:textfield label=<span class="java&#45;quote">"Username"</span> name=<span class="java&#45;quote">"username"</span>/&#62;
&#60;ui:password label=<span class="java&#45;quote">"Password"</span> name=<span class="java&#45;quote">"password"</span>/&#62;
&#60;ui:component template=<span class="java&#45;quote">"/mytemplate.vm"</span>&#62;
  &#60;ui:param name=<span class="java&#45;quote">"param1"</span> value=<span class="java&#45;quote">"value1"</span>/&#62;
&#60;/ui&#62;
&#60;ui:submit value=<span class="java&#45;quote">"'login'"</span> align=<span class="java&#45;quote">"right"</span>/&#62;</pre></div><p class="paragraph"/>Now, let's say we want to render these pages using Velocity rather than JSP.<p class="paragraph"/><div class="code"><pre>#tag( TextField <span class="java&#45;quote">"label=Username"</span> <span class="java&#45;quote">"name=username"</span> )
#tag( Password <span class="java&#45;quote">"label=Password"</span> <span class="java&#45;quote">"name=password"</span> )
#bodytag( Component <span class="java&#45;quote">"template=/mytemplate.vm"</span> )
  #param( <span class="java&#45;quote">"name=param1"</span> <span class="java&#45;quote">"value=value1"</span> )
#end
#tag( Submit <span class="java&#45;quote">"value='login'"</span> <span class="java&#45;quote">"align=right"</span> )</pre></div><p class="paragraph"/>That's it!
<h3 class="heading-1">Down the rabbit hole
</h3><p class="paragraph"/>Let's now dissect what's going on with this example.  To access the UI templates from Velocity, you can use one of two provided directives, #tag and #bodytag.  They are functionally equivalent, however #bodytag allows you to pass additional parameters to the tag.<p class="paragraph"/>The general syntax is<p class="paragraph"/> #tag( <b class="bold">name-of-tag list-of-attributes</b> )<p class="paragraph"/>or<p class="paragraph"/> #bodytag( <b class="bold">name-of-tag</b> <b class="bold">list-of-attributes</b> ) <br/>
 <b class="bold">one-or-more #param tags</b> <br/>
 #end<p class="paragraph"/>where
<ul class="star">
<li><b class="bold">name-of-tag</b> is the name of the UI tag that you'd like to use</li>
<li><b class="bold">list-of-attributes</b> are a list of 0 or more attributes in "name=value" format</li>
</ul>
<h3 class="heading-1">More Examples
</h3><p class="paragraph"/>Here's the code from the WebWork 2 example that shows a side by side comparison of using the Tags in a JSP and a Velocity Template.<p class="paragraph"/>JSP:<p class="paragraph"/><div class="code"><pre>&#60;ww:label label=<span class="java&#45;quote">"textfield test"</span> name=<span class="java&#45;quote">"textfieldName"</span> value=<span class="java&#45;quote">"scalar"</span>/&#62;
&#60;ww:textfield label=<span class="java&#45;quote">"textfield test"</span> name=<span class="java&#45;quote">"textfieldName"</span> value=<span class="java&#45;quote">"scalar"</span> size=<span class="java&#45;quote">"50"</span>/&#62;
&#60;ww:password label=<span class="java&#45;quote">"password test"</span> name=<span class="java&#45;quote">"passwordField"</span> value=<span class="java&#45;quote">"scalar"</span> size=<span class="java&#45;quote">"50"</span>/&#62;
&#60;ww:checkbox label=<span class="java&#45;quote">"checkbox test"</span> name=<span class="java&#45;quote">"checkboxField"</span>/&#62;
&#60;ww:component label=<span class="java&#45;quote">"component test"</span> name=<span class="java&#45;quote">"componentField"</span> template=<span class="java&#45;quote">"/decorators/xhtml/empty.vm"</span> /&#62;
&#60;ww:radio label=<span class="java&#45;quote">"radio test"</span> name=<span class="java&#45;quote">"RadioField"</span> value=<span class="java&#45;quote">"list&#91;1&#93;.value"</span> list=<span class="java&#45;quote">"list"</span> listKey=<span class="java&#45;quote">"key"</span> listValue=<span class="java&#45;quote">"value"</span>/&#62;
&#60;ww:select label=<span class="java&#45;quote">"multiple select test"</span> name=<span class="java&#45;quote">"select1"</span> value=<span class="java&#45;quote">"multiValues"</span> list=<span class="java&#45;quote">"multiList"</span> multiple=<span class="java&#45;quote">"<span class="java&#45;keyword">true</span>"</span> size=<span class="java&#45;quote">"5"</span>/&#62;
&#60;ww:select label=<span class="java&#45;quote">"select test"</span> name=<span class="java&#45;quote">"select2"</span> value=<span class="java&#45;quote">"list&#91;1&#93;.value"</span> list=<span class="java&#45;quote">"list"</span> listKey=<span class="java&#45;quote">"key"</span> listValue=<span class="java&#45;quote">"value"</span> size=<span class="java&#45;quote">"5"</span>/&#62;
&#60;ww:select label=<span class="java&#45;quote">"pulldown test"</span> name=<span class="java&#45;quote">"select3"</span> value=<span class="java&#45;quote">"list&#91;1&#93;.value"</span> list=<span class="java&#45;quote">"list"</span> listKey=<span class="java&#45;quote">"key"</span> listValue=<span class="java&#45;quote">"value"</span>/&#62;
&#60;ww:textarea rows=<span class="java&#45;quote">"10"</span> cols=<span class="java&#45;quote">"30"</span> label=<span class="java&#45;quote">"textarea test"</span> name=<span class="java&#45;quote">"textareaField"</span> value=<span class="java&#45;quote">"scalar"</span> /&#62;
&#60;ww:hidden name=<span class="java&#45;quote">"hiddenField"</span> value=<span class="java&#45;quote">"scalar"</span>/&#62;
&#60;ww:submit value=<span class="java&#45;quote">"'submit <span class="java&#45;keyword">this</span> thang!'"</span> align=<span class="java&#45;quote">"right"</span>/&#62;</pre></div><p class="paragraph"/>Velocity:<p class="paragraph"/><div class="code"><pre>#tag( Label <span class="java&#45;quote">"label=label test"</span> <span class="java&#45;quote">"name='label name'"</span> <span class="java&#45;quote">"value=scalar"</span> )
#tag( TextField <span class="java&#45;quote">"label=textfield test"</span> <span class="java&#45;quote">"name=textfieldName"</span> <span class="java&#45;quote">"value=scalar"</span> <span class="java&#45;quote">"size=50"</span> )
#tag( File <span class="java&#45;quote">"label=file test"</span> <span class="java&#45;quote">"name=uploadedFile"</span> <span class="java&#45;quote">"value="</span> <span class="java&#45;quote">"size=30"</span> )
#tag( Password <span class="java&#45;quote">"label=password test"</span> <span class="java&#45;quote">"name=passwordField"</span> <span class="java&#45;quote">"value=scalar"</span> <span class="java&#45;quote">"size=50"</span> )
#tag( Checkbox <span class="java&#45;quote">"label=checkbox test"</span> <span class="java&#45;quote">"name=checkboxField"</span> )
#tag( Component <span class="java&#45;quote">"label=component test"</span> <span class="java&#45;quote">"name=componentField"</span> <span class="java&#45;quote">"template=/templates/xhtml/empty.vm"</span> )
#tag( Radio <span class="java&#45;quote">"label=radio test"</span> <span class="java&#45;quote">"name=radioField"</span> <span class="java&#45;quote">"value=list&#91;1&#93;.value"</span> <span class="java&#45;quote">"list=list"</span> <span class="java&#45;quote">"listKey=key"</span> <span class="java&#45;quote">"listValue=value"</span> )
#tag( Select <span class="java&#45;quote">"label=multiple select test"</span> <span class="java&#45;quote">"name=select1"</span> <span class="java&#45;quote">"value=multiValues"</span> <span class="java&#45;quote">"list=multiList"</span> <span class="java&#45;quote">"multiple=<span class="java&#45;keyword">true</span>"</span> <span class="java&#45;quote">"size=5"</span> )
#tag( Select <span class="java&#45;quote">"label=select test"</span> <span class="java&#45;quote">"name=select2"</span> <span class="java&#45;quote">"value=list&#91;1&#93;.value"</span> <span class="java&#45;quote">"list=list"</span> <span class="java&#45;quote">"listKey=key"</span> <span class="java&#45;quote">"listValue=value"</span> <span class="java&#45;quote">"size=5"</span> )
#tag( Select <span class="java&#45;quote">"label=pulldown test"</span> <span class="java&#45;quote">"name=select3"</span> <span class="java&#45;quote">"value=list&#91;1&#93;.value"</span> <span class="java&#45;quote">"list=list"</span> <span class="java&#45;quote">"listKey=key"</span> <span class="java&#45;quote">"listValue=value"</span> )
#tag( Textarea <span class="java&#45;quote">"label=textarea test"</span> <span class="java&#45;quote">"name=textareaField"</span> <span class="java&#45;quote">"value=scalar"</span> <span class="java&#45;quote">"rows=10"</span> <span class="java&#45;quote">"cols=30"</span> )
#tag( Hidden <span class="java&#45;quote">"name=hiddenField"</span> <span class="java&#45;quote">"value=scalar"</span> )
#tag( Submit <span class="java&#45;quote">"value='submit <span class="java&#45;keyword">this</span> thang!'"</span> <span class="java&#45;quote">"align=right"</span> )</pre></div><p class="paragraph"/>
  </div>
</body>
</html>