WebWork 2 : Non-UI Tags
This page last changed on Dec 14, 2004 by plightbo.
Common Tags<ww:param />Allows you to add parameters to tags that support adding parametric tags.
You can place param tags within the body of parametric supporting tags and param will add its parameter to its parent. It evaluates the body as the value if no value is given. In this example, each param will add its parameter to Counter. This means param will call Counter's appropriate setter method. <ww:bean name="'webwork.util.Counter'" id="year"> <ww:param name="'first'" value="text('firstBirthYear')"/> <ww:param name="'last'" value="2000"/> <ui:combobox label="'Birth year'" size="6" maxlength="4" name="'birthYear'" list="#year"/> </ww:bean> <ww:property />The property tag fetches a value and prints it
Some examples will illustrate these different uses: Print getX().getY()
<ww:property value="x.y"/>
HTML characters will be escaped by default, whereas the contents of property tags with bodies will not be escaped. This behavior can be overridden by explicitly setting the escape attribute. Quoted text that is escaped will have its outer quotes stripped. Note also that if the property tag has an empty body, it behaves the same as having no body and prints the value, though both spaces and carriage returns constitute nonempty content.<ww:push />Using ww:push, you can add an object of your choice to the top of the value stack.
This is similar to what you can do with ww:set (see below), so read both before deciding which to use. <ww:push value="counter"> <ww:property value="count"/> </ww:push> To make an action available on the stack: <ww:action name="'SomeAction'" id="sa"/> <ww:push value="#sa"> foo = <ww:property value="foo"/> </ww:push> <ww:set />You can create your own named variables from within a JSP using the ww:set tag. Reference your variable later using the # variableName notation.
Sets the value of an object in the VS to a scope. If the value is not given, the top of the stack is used. If the scope is not given, the default scope is the action context which is only available in the PageContext if no action has been executed on the same request. Componentisation Tagsreturn to top<ww:action />Action tag provides another method to call Actions. This is an alternative way to invoke an action besides calling an url; i.e. - *.action that would be sent to the ServletDispatcher.
If the id attribute is given, the executed action is assigned a name reference that can be later retrieved from the context "#id". In this example, the ClientInfo action will be executed and its methods will be used to retrieve information and perform a conditional test.<ww:action name="'ClientInfo'" id="cinfo"> <ww:param name="detailedMode" value="false"/> </ww:action> Browser:<ww:property value="#cinfo.browser"/><br> Version:<ww:property value="#cinfo.version"/><br> Supports GIF:<ww:if test="#cinfo.supportsType('image/gif') == true">Yes</ww:if> <ww:else>No</ww:else><br> <ww:bean />Create a JavaBean and instantiate its properties. It is then placed in the ActionContext for later use.
In this example, Counter is used as a bean. We can now call the methods we desire. In this case, we setFirst() to first birth year which is 1975 and we setLast() to 2000. We then display a combo box using Counter as an Iterator. <ww:bean name="'webwork.util.Counter'" id="year"> <ww:param name="'first'" value="text('firstBirthYear')"/> <ww:param name="'last'" value="2000"/> <ui:combobox label="'Birth year'" size="6" maxlength="4" name="'birthYear'" list="#year"/> </ww:bean> <ww:include />Used to include another page or action.
In this example, beaninfo.jsp will introspec on people[0] which is a Person. Take a look at beaninfo.jsp example and notice how it retrieves the parent value off the ValueStack with "..". <ww:property value="people[0]"> <ww:include value="'beaninfo.jsp'"/> </ww:property> <h1>RSS viewer</h1>
<ww:include value="'rss.viewer.action'"/>
Flow Control Tagsreturn to top<ww:if />Used to determine if a statement is true or false.
In this example, if will evaluate its body since the test condition is true. elseIf and else will not evaluate. <ww:if test="true == true"> <b>if: Success</b> </ww:if> <ww:elseIf test="true == true"> <b>elseIf: Failure</b> </ww:elseIf> <ww:else> <b>else: Failure</b> </ww:else> <ww:elseif />Used to determine if a statement is true or false after a previous test.
In this example, elseIf will evaluate its body since its test condition is true and if is false. <ww:if test="true == false"> <b>if: Failures</b> </ww:if> <ww:elseIf test="true == true"> <b>elseIf: Success</b> </ww:elseIf> <ww:else> <b>else: Failure</b> </ww:else> <ww:else />Used to determine if the preceding statement was false.
In this exmaple, else will evaluate its body since both if and elseIf conditions are false. <ww:if test="true == false"> <b>if: Failures</b> </ww:if> <ww:elseIf test="true == false"> <b>elseIf: Failure</b> </ww:elseIf> <ww:else> <b>else: Success</b> </ww:else> Iteration Tagsreturn to top<ww:iterator />Iterator will iterate over a value. An iterable value can be either of: java.util.Collection, java.util.Iterator, java.util.Enumeration, java.util.Map, array, XML Node, or XML NodeList.
In this example, iterator will iterate over Counter. property will output the current value which is 1 through 10. <ww:bean name="'webwork.util.Counter'"> <ww:param name="'last'" value="10"/> <ww:iterator> <ww:property/><br /> </ww:iterator> </ww:bean> In this example, we use a couple of IteratorStatus to see where we are within iterations. <h1>Testing iterator status</h1> <ww:bean name="'webwork.util.Counter'" id="rowcounter"> <ww:param name="'first'" value="0"/> <ww:param name="'last'" value="5"/> </ww:bean> <table border="1"> <ww:iterator value="#rowcounter" status="rowstatus"> <tr> <ww:bean name="'webwork.util.Counter'" id="colcounter"> <ww:param name="'first'" value="0"/> <ww:param name="'last'" value="5"/> </ww:bean> <ww:iterator value="#colcounter" status="colstatus"> <!-- if it is (first row) or (first column) or (last row) then output the column number. --> <ww:if test="#rowstatus.first==true || #colstatus.first==true || #rowstatus.last==true"> <th><ww:property value="#colstatus.count"/></th> </ww:if> <ww:else> <td><ww:property/></td> </ww:else> </ww:iterator> </tr> </ww:iterator> </table> Here we use the IteratorStatus determine every other row to insert an extra line break. This is very useful for shading alternate rows in an HTML table. Both even and odd attributes are available. <ww:iterator status="status"> <ww:if test="#status.odd == true"> <br /> </ww:if> <br /> </ww:iterator> Here we use the IteratorStatus determine every fourth row to insert an extra line break. <ww:iterator status="status"> <ww:if test="#status.modulus(4) == 0"> <br /> </ww:if> <br /> </ww:iterator> Following are the list of operations available on the status object:
<ww:generator />Generate will create Iterators from val.
In this example, two Iterators are created. One for val="'foo,bar,xyzzy'" and the other for val="' '". <h1>Testing append, subset, and value generators</h1> <table border="1"> <ww:bean name="'webwork.util.Counter'"> <ww:param name="'last'" value="5"/> <ww:iterator id="colcount"> <tr> <!-- Generator will create an Iterator that has 5 items. The first 3 are "foo,bar,xyzzy". Item 4 and 5 will be foo and bar respectively. If the count is more than the items, you start over. --> <ww:generator val="'foo,bar,xyzzy'" separator="','" count="#colcount" id="values"/> <!-- Generator will create an Iterator that has infinite . Count=-1 means indefinite. --> <ww:generator val="' '" count="-1" id="space"/> <ww:append> <ww:param name="'source'" value="#values"/> <ww:param name="'source'" value="#space"/> <ww:subset count="6"> <ww:iterator> <td width="40"><ww:property/></td> </ww:iterator> </iterator:subset> </iterator:append> </tr> </ww:iterator> </ww:bean> </table>
<ww:iterator value="{1, 2, 3, 4}">
</ww:iterator>
<ww:append />Append will append a list of iterators. The values of the iterators will be appended and treated as one iterator. The outputs from the iterator will be in the sequence the sources were added.
In this example, the two iterators #values and #spaces are appended. This means #spaces values are after #values. <h1>Testing append, subset, and value generators</h1> <table border="1"> <ww:bean name="'webwork.util.Counter'"> <ww:param name="'last'" value="5"/> <ww:iterator id="colcount"> <tr> <ww:generator val="'foo,bar,xyzzy'" separator="','" count="#colcount" id="values"/> <ww:generator val="' '" count="-1" id="space"/> <ww:append> <ww:param name="'source'" value="#values"/> <ww:param name="'source'" value="#space"/> <ww:subset count="6"> <ww:iterator> <td width="40"><ww:property/></td> </ww:iterator> </iterator:subset> </iterator:append> </tr> </ww:iterator> </ww:bean> </table> <ww:subset />Subset will iterate over a portion of its source. It will start at start and continue for count. You can set count to -1 if you want to iterate until the end of source. If you do not supply a source, the current object on the ValueStack- "." will be used.
In this example, subset will iterate over 6 items for the current object in the ValueStack. <h1>Testing append, subset, and value generators</h1> <table border="1"> <ww:bean name="'webwork.util.Counter'"> <ww:param name="'last'" value="5"/> <ww:iterator id="colcount"> <tr> <ww:generator val="'foo,bar,xyzzy'" separator="','" count="#colcount" id="values"/> <ww:generator val="' '" count="-1" id="space"/> <ww:append> <ww:param name="'source'" value="#values"/> <ww:param name="'source'" value="#space"/> <ww:subset count="6"> <ww:iterator> <td width="40"><ww:property/></td> </ww:iterator> </iterator:subset> </iterator:append> </tr> </ww:iterator> </ww:bean> </table> <ww:merge />Merge several iterators into one. It weaves them together. If one iterator runs out, it will drop off and the others will continue weaving until there are no more values.
In this example, #foo, #bar, and #xyzzy iterators are merged together. So, the output will be foo, bar, xyzzy until #foo and #xyzzy iterators run out in which case #bar will finish. Three value generators with merge and subset limits:<br> <ww:generator val="'foo'" count="5" id="foo"/> <ww:generator val="'bar'" count="10" id="bar"/> <ww:generator val="'xyzzy'" count="5" id="xyzzy"/> <ww:merge> <ww:param name="'source'" value="#foo"/> <ww:param name="'source'" value="#bar"/> <ww:param name="'source'" value="#xyzzy"/> <ww:subset count="30"> <ww:iterator status="status"> <ww:property value="#status.count"/>:<ww:property/><br> </ww:iterator> </iterator:subset> </iterator:merge> <ww:sort />Sort allows you to sort an iterator. It uses Collections.sort() given the comparator you supply.
In this example, we sort ascending. <ww:bean name="'webwork.util.Counter'" id="counter"> <ww:param name="'first'" value="0"/> <ww:param name="'last'" value="5"/> </ww:bean> <ww:bean name="'webwork.util.Sorter'" id="sorter"/> Ascending:<br /> <ww:sort source="#counter" comparator="#sorter.ascending"> <ww:iterator> <ww:property/><br /> </ww:iterator> </iterator:sort> In this example, we sort descending. <ww:bean name="'webwork.util.Sorter'" id="sorter"/> <ww:bean name="'webwork.util.Counter'" id="counter"> <ww:param name="'first'" value="0"/> <ww:param name="'last'" value="5"/> </ww:bean> Descending:<br> <ww:sort source="#counter" comparator="#sorter.descending"> <ww:iterator> <ww:property/><br> </ww:iterator> </iterator:sort> In this example, we sort ascending over strings. <ww:bean name="'webwork.util.Sorter'" id="sorter"/> Sorting strings:<br> <ww:generator val="'Rickard,Maurice,Hristo'" separator="','" id="names"/> <ww:sort source="#names" comparator="#sorter.ascending"> <ww:iterator> <ww:property/><br> </ww:iterator> </iterator:sort> Notes![]() ![]() So, if you instantiate a bean with the bean tag (<ww:bean name="'br.univap.fcc.sgpw.util.FormattersHelper'">) that bean will be avaliable on the valuestack only until the </ww:bean> tag. |
![]() |
Document generated by Confluence on Dec 14, 2004 16:36 |