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 construct a
freemarker.ext.servlet.HttpRequestParametersHashModel, which
wraps form parameters back into your data model so they can be used to
set default values of form fields. Parameters with single values are wrapped
as scalars; parameters with multiple values are wrapped as lists of scalars,
with the first value wrapped as a scalar. For instance, if you created a
HttpRequestParametersHashModel and put it into your data model
with the name "request", 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 |