Sometimes it's useful for a template to access the request variables that were submitted in a form. For example, sometimes you want to redisplay the form with user's input still in the fields. If you're writing such a page, you can use the static copyRequest() method of class TemplateServletUtils, which copies form parameters back into your data model so they can be used to set default values of form fields. All parameters are copied into a hash called request. Parameters with single values are copied as scalars; parameters with multiple values are copied as lists of scalars. Here's an example of how you could use this to redisplay a text field and give it the submitted value:
<input type="text" name="foo" size="20" value="${request.foo}">
Here's an example with checkboxes:
<input type="checkbox" name="foo" value="checked" ${request.foo}>Foo<br> <input type="checkbox" name="bar" value="checked" ${request.bar}>Bar<br>
With radio buttons:
<input type="radio" name="foo" value="yes" <if request.foo == "yes">checked</if>>Yes<br> <input type="radio" name="foo" value="no" <if request.foo == "no">checked</if>>No<br>
A single-selection pull-down menu:
<select name="foo"> <option value="yes" <if request.foo == "yes">selected</if>>Yes <option value="no" <if request.foo == "no">selected</if>>No </select>
A single-selection pull-down menu whose values come from your data model:
<select name="foo"> <list fooOptions as fooOption> <option value="${fooOption}" <if request.foo == fooOption>selected</if>>${fooOption} </list> </select>
A multiple-selection pull-down menu whose values come from your data model:
<select name="foo" size="5" multiple> <list fooOptions as fooOption> <option value="${fooOption}" <list request.foo as fooSelection> <if fooOption == fooSelection>selected</if> </list> >${fooOption} </list> </select>
Since a scalar can be treated as a list with one element, this will work even if the user selects only one value.
Previous: Template Caches | Next: JDK 1.1 Instructions |