@@ -35,7 +35,7 @@ See https://www.thymeleaf.org/documentation.html[Thymeleaf+Spring] for more deta
35
35
[.small]#<<web-reactive.adoc#webflux-view-freemarker, Same as in Spring WebFlux>>#
36
36
37
37
https://freemarker.apache.org/[Apache FreeMarker] is a template engine for generating any
38
- kind of text output from HTML to email and others. The Spring Framework has a built-in
38
+ kind of text output from HTML to email and others. The Spring Framework has built-in
39
39
integration for using Spring MVC with FreeMarker templates.
40
40
41
41
@@ -98,8 +98,9 @@ properties, as the following example shows:
98
98
----
99
99
100
100
Your templates need to be stored in the directory specified by the `FreeMarkerConfigurer`
101
- shown in the preceding example. Given the preceding configuration, if your controller returns a view name
102
- of `welcome`, the resolver looks for the `/WEB-INF/freemarker/welcome.ftl` template.
101
+ shown in the preceding example. Given the preceding configuration, if your controller
102
+ returns a view name of `welcome`, the resolver looks for the
103
+ `/WEB-INF/freemarker/welcome.ftl` template.
103
104
104
105
105
106
@@ -108,10 +109,10 @@ of `welcome`, the resolver looks for the `/WEB-INF/freemarker/welcome.ftl` templ
108
109
[.small]#<<web-reactive.adoc#webflux-views-freemarker, Same as in Spring WebFlux>>#
109
110
110
111
You can pass FreeMarker 'Settings' and 'SharedVariables' directly to the FreeMarker
111
- `Configuration` object (which is managed by Spring) by setting the appropriate bean properties on
112
- the `FreeMarkerConfigurer` bean. The `freemarkerSettings` property requires a
113
- `java.util.Properties` object, and the `freemarkerVariables` property requires a
114
- `java.util.Map`. The following example shows how to do so :
112
+ `Configuration` object (which is managed by Spring) by setting the appropriate bean
113
+ properties on the `FreeMarkerConfigurer` bean. The `freemarkerSettings` property requires
114
+ a `java.util.Properties` object, and the `freemarkerVariables` property requires a
115
+ `java.util.Map`. The following example shows how to use a `FreeMarkerConfigurer` :
115
116
116
117
[source,xml,indent=0]
117
118
[subs="verbatim,quotes"]
@@ -145,42 +146,43 @@ with additional convenience macros for generating form input elements themselves
145
146
146
147
[[mvc-view-bind-macros]]
147
148
==== The Bind Macros
149
+ [.small]#<<web-reactive.adoc#webflux-view-bind-macros, Same as in Spring WebFlux>>#
148
150
149
- A standard set of macros are maintained within the `spring-webmvc.jar` file for both
150
- languages , so they are always available to a suitably configured application.
151
+ A standard set of macros are maintained within the `spring-webmvc.jar` file for
152
+ FreeMarker , so they are always available to a suitably configured application.
151
153
152
- Some of the macros defined in the Spring libraries are considered internal (private), but
153
- no such scoping exists in the macro definitions, making all macros visible to calling
154
- code and user templates. The following sections concentrate only on the macros you need
155
- to directly call from within your templates. If you wish to view the macro code
154
+ Some of the macros defined in the Spring templating libraries are considered internal
155
+ (private), but no such scoping exists in the macro definitions, making all macros visible
156
+ to calling code and user templates. The following sections concentrate only on the macros
157
+ you need to directly call from within your templates. If you wish to view the macro code
156
158
directly, the file is called `spring.ftl` and is in the
157
159
`org.springframework.web.servlet.view.freemarker` package.
158
160
159
161
160
162
[[mvc-view-simple-binding]]
161
163
==== Simple Binding
162
164
163
- In your HTML forms (vm or ftl templates) that act as a form view for a Spring MVC
165
+ In your HTML forms based on FreeMarker templates that act as a form view for a Spring MVC
164
166
controller, you can use code similar to the next example to bind to field values and
165
- display error messages for each input field in similar fashion to the JSP equivalent.
166
- The following example shows the `personForm` view that was configured earlier :
167
+ display error messages for each input field in similar fashion to the JSP equivalent. The
168
+ following example shows a `personForm` view:
167
169
168
170
[source,xml,indent=0]
169
171
[subs="verbatim,quotes"]
170
172
----
171
- <!-- freemarker macros have to be imported into a namespace. We strongly
172
- recommend sticking to 'spring' -->
173
+ <!-- FreeMarker macros have to be imported into a namespace.
174
+ We strongly recommend sticking to 'spring'. -->
173
175
<#import "/spring.ftl" as spring/>
174
176
<html>
175
177
...
176
178
<form action="" method="POST">
177
179
Name:
178
- <@spring.bind "myModelObject .name"/>
180
+ <@spring.bind "personForm .name"/>
179
181
<input type="text"
180
182
name="${spring.status.expression}"
181
- value="${spring.status.value?html}"/><br>
182
- <#list spring.status.errorMessages as error> <b>${error}</b> <br> </#list>
183
- <br>
183
+ value="${spring.status.value?html}"/><br / >
184
+ <#list spring.status.errorMessages as error> <b>${error}</b> <br / > </#list>
185
+ <br / >
184
186
...
185
187
<input type="submit" value="submit"/>
186
188
</form>
@@ -189,29 +191,29 @@ The following example shows the `personForm` view that was configured earlier:
189
191
----
190
192
191
193
`<@spring.bind>` requires a 'path' argument, which consists of the name of your command
192
- object (it is 'command', unless you changed it in your `FormController` properties)
193
- followed by a period and the name of the field on the command object to which you wish to bind.
194
- You can also use nested fields, such as `command.address.street`. The `bind` macro assumes
195
- the default HTML escaping behavior specified by the ServletContext parameter
194
+ object (it is 'command', unless you changed it in your controller configuration) followed
195
+ by a period and the name of the field on the command object to which you wish to bind. You
196
+ can also use nested fields, such as `command.address.street`. The `bind` macro assumes the
197
+ default HTML escaping behavior specified by the ` ServletContext` parameter
196
198
`defaultHtmlEscape` in `web.xml`.
197
199
198
- The optional form of the macro called `<@spring.bindEscaped>` takes a second argument
199
- and explicitly specifies whether HTML escaping should be used in the status error
200
- messages or values. You can set it to `true` or `false` as required. Additional form handling macros
201
- simplify the use of HTML escaping, and you should use these macros wherever possible.
202
- They are explained in the next section.
200
+ An alternative form of the macro called `<@spring.bindEscaped>` takes a second argument
201
+ that explicitly specifies whether HTML escaping should be used in the status error
202
+ messages or values. You can set it to `true` or `false` as required. Additional form
203
+ handling macros simplify the use of HTML escaping, and you should use these macros
204
+ wherever possible. They are explained in the next section.
203
205
204
206
205
207
[[mvc-views-form-macros]]
206
- ==== Input macros
208
+ ==== Input Macros
207
209
208
- Additional convenience macros for both languages simplify both binding and form
209
- generation (including validation error display). It is never necessary to use these
210
- macros to generate form input fields, and you can mix and match them with simple HTML
211
- or direct calls to the spring bind macros that we highlighted previously.
210
+ Additional convenience macros for FreeMarker simplify both binding and form generation
211
+ (including validation error display). It is never necessary to use these macros to
212
+ generate form input fields, and you can mix and match them with simple HTML or direct
213
+ calls to the Spring bind macros that we highlighted previously.
212
214
213
- The following table of available macros shows the FTL definitions and the
214
- parameter list that each takes:
215
+ The following table of available macros shows the FreeMarker Template (FTL) definitions
216
+ and the parameter list that each takes:
215
217
216
218
[[views-macros-defs-tbl]]
217
219
.Table of macro definitions
@@ -263,9 +265,9 @@ parameter list that each takes:
263
265
| <@spring.showErrors separator, classOrStyle/>
264
266
|===
265
267
266
- * In FTL ( FreeMarker) , `formHiddenInput` and `formPasswordInput` are not actually required,
267
- as you can use the normal `formInput` macro, specifying `hidden` or `password` as the
268
- value for the `fieldType` parameter.
268
+ NOTE: In FreeMarker templates , `formHiddenInput` and `formPasswordInput` are not actually
269
+ required, as you can use the normal `formInput` macro, specifying `hidden` or `password`
270
+ as the value for the `fieldType` parameter.
269
271
270
272
The parameters to any of the above macros have consistent meanings:
271
273
@@ -290,8 +292,7 @@ The parameters to any of the above macros have consistent meanings:
290
292
element that wraps each error uses. If no information is supplied (or the value is
291
293
empty), the errors are wrapped in `<b></b>` tags.
292
294
293
- The following sections outline examples of the macros (some in FTL and some in VTL). Where usage
294
- differences exist between the two languages, they are explained in the notes.
295
+ The following sections outline examples of the macros.
295
296
296
297
[[mvc-views-form-macros-input]]
297
298
===== Input Fields
@@ -300,7 +301,7 @@ The `formInput` macro takes the `path` parameter (`command.name`) and an additio
300
301
parameter (which is empty in the upcoming example). The macro, along with all other form
301
302
generation macros, performs an implicit Spring bind on the path parameter. The binding
302
303
remains valid until a new bind occurs, so the `showErrors` macro does not need to pass the
303
- path parameter again -- it operates on the field for which a bind was last created.
304
+ path parameter again -- it operates on the field for which a binding was last created.
304
305
305
306
The `showErrors` macro takes a separator parameter (the characters that are used to
306
307
separate multiple errors on a given field) and also accepts a second parameter -- this
@@ -333,7 +334,7 @@ The generated HTML resembles the following example:
333
334
----
334
335
335
336
The `formTextarea` macro works the same way as the `formInput` macro and accepts the same
336
- parameter list. Commonly, the second parameter (attributes) is used to pass style
337
+ parameter list. Commonly, the second parameter (` attributes` ) is used to pass style
337
338
information or `rows` and `cols` attributes for the `textarea`.
338
339
339
340
[[mvc-views-form-macros-select]]
@@ -367,7 +368,7 @@ model under the name 'cityMap'. The following listing shows the example:
367
368
The preceding listing renders a line of radio buttons, one for each value in `cityMap`, and uses a
368
369
separator of `""`. No additional attributes are supplied (the last parameter to the macro is
369
370
missing). The `cityMap` uses the same `String` for each key-value pair in the map. The map's
370
- keys are what the form actually submits as POSTed request parameters. The map values are the
371
+ keys are what the form actually submits as `POST` request parameters. The map values are the
371
372
labels that the user sees. In the preceding example, given a list of three well known cities
372
373
and a default value in the form backing object, the HTML resembles the following:
373
374
@@ -414,7 +415,7 @@ user still sees the more user-friendly city names, as follows:
414
415
[[mvc-views-form-macros-html-escaping]]
415
416
==== HTML Escaping
416
417
417
- Default usage of the form macros described earlier results in HTML elemets that are HTML 4.01
418
+ Default usage of the form macros described earlier results in HTML elements that are HTML 4.01
418
419
compliant and that use the default value for HTML escaping defined in your `web.xml` file, as
419
420
used by Spring's bind support. To make the elements be XHTML compliant or to override
420
421
the default HTML escaping value, you can specify two variables in your template (or in
@@ -432,8 +433,8 @@ model or context variable named `xhtmlCompliant`, as the following example shows
432
433
<#assign xhtmlCompliant = true>
433
434
----
434
435
435
- After processing
436
- this directive, any elements generated by the Spring macros are now XHTML compliant.
436
+ After processing this directive, any elements generated by the Spring macros are now XHTML
437
+ compliant.
437
438
438
439
In similar fashion, you can specify HTML escaping per field, as the following example shows:
439
440
0 commit comments