...
The component listed below is a looping component; it renders its body a number of times, defined by its start and end parameters (which set the boundaries of the loop). The component can update a value result parameter bound to a property of its container, ; it will automatically count up or down depending on whether start or end is larger.
Code Block | ||||
---|---|---|---|---|
| ||||
package org.example.app.components; import org.apache.tapestry5.annotations.AfterRender; import org.apache.tapestry5.annotations.Parameter; import org.apache.tapestry5.annotations.SetupRender; public class Count { @Parameter (value="1") private int start = 1; @Parameter(required = true) private int end; @Parameter private int valueresult; private boolean increment; @SetupRender void initializeValueinitializeValues() { valueresult = start; increment = start < end; } @AfterRender boolean next() { if (increment) { int newValuenewResult = value + 1; if (newValuenewResult <= end) { valueresult = newValuenewResult; return false; } } else { int newValue newResult= value - 1; if (newValue >newResult>= end) { valueresult = newValuenewResult; return false; } } return true; } } |
The name of the parameter is derived from the same as field name (by stripping except with leading "_" and "$" characters, if any, removed). Here, the parameter names are "start", "end" and "valueresult".
Anchor | ||||
---|---|---|---|---|
|
...
Prefix | Description |
asset: | The relative path to an asset file (which must exist) |
block: | The id of a block within the template |
component: | The id of another component within the same template |
context: | Context asset: path from context root |
literal: | A literal string. |
nullfieldstrategy: | Used to locate a pre-defined NullFieldStrategy |
message: | Retrieves a value string from the component's message catalog |
prop: | A property expression to read or update |
symbol: | Used to read one of your symbols |
translate: | The name of a configured translator |
validate: | A validator specification used to create some number of field validators |
var: | Allows a render variable of the component to be read or updated |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> <p> Countdown: <t:count start="5" end="1" valueresult="index"> ${index} ... </t:count> </p> </t:layout> |
Because the Count component updates its value result parameter (the _value result
field), the index property of the containing component is updated. Inside the Count's body, we output the current value of the index property, using the expansion ${index
}. The resulting output will look something like:
...
You may set a default value for optional parameters as you would for any other fieldusing the value
element of the @Parameter annotation. In the Count component above, the min start parameter has a default value of 1. That value is used unless the min start parameter is bound, in which case, the bound value supersedes the default.
...
The @Parameter annotation's value
() attribute element can be used to specify a binding expression that will be the default binding for the parameter is if otherwise left unbound. Typically, this is the name of a property that that will compute the value on the fly.
Example:
Code Block | ||||
---|---|---|---|---|
| ||||
@Parameter(value="defaultMessage") // or, equivalently, @Parameter("defaultMessage") private String message; @Parameter(required=true) private int maxLength; public String getDefaultMessage() { return String.format("Maximum field length is %d.", _maxLength); } |
...