Skip to content

Commit c4075cf

Browse files
committed
Document WebFlux FreeMarker macro support in Reference Manual
Closes gh-23133
1 parent eef9bc8 commit c4075cf

File tree

2 files changed

+98
-56
lines changed

2 files changed

+98
-56
lines changed

src/docs/asciidoc/web/webflux-view.adoc

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ http://forum.thymeleaf.org/Thymeleaf-3-0-8-JUST-PUBLISHED-td4030687.html[announc
3636
[.small]#<<web.adoc#mvc-view-freemarker, Same as in Spring MVC>>#
3737

3838
https://freemarker.apache.org/[Apache FreeMarker] is a template engine for generating any
39-
kind of text output from HTML to email and others. The Spring Framework has a built-in
39+
kind of text output from HTML to email and others. The Spring Framework has built-in
4040
integration for using Spring WebFlux with FreeMarker templates.
4141

4242

@@ -64,15 +64,15 @@ The following example shows how to configure FreeMarker as a view technology:
6464
@Bean
6565
public FreeMarkerConfigurer freeMarkerConfigurer() {
6666
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
67-
configurer.setTemplateLoaderPath("classpath:/templates");
67+
configurer.setTemplateLoaderPath("classpath:/templates/freemarker");
6868
return configurer;
6969
}
7070
}
7171
----
7272

7373
Your templates need to be stored in the directory specified by the `FreeMarkerConfigurer`,
74-
shown in the preceding example. Given the preceding configuration, if your controller returns the view name,
75-
`welcome`, the resolver looks for the
74+
shown in the preceding example. Given the preceding configuration, if your controller
75+
returns the view name, `welcome`, the resolver looks for the
7676
`classpath:/templates/freemarker/welcome.ftl` template.
7777

7878

@@ -82,9 +82,9 @@ shown in the preceding example. Given the preceding configuration, if your contr
8282
[.small]#<<web.adoc#mvc-views-freemarker, Same as in Spring MVC>>#
8383

8484
You can pass FreeMarker 'Settings' and 'SharedVariables' directly to the FreeMarker
85-
`Configuration` object (managed by Spring) by setting the appropriate bean properties on
86-
the `FreeMarkerConfigurer` bean. The `freemarkerSettings` property requires a
87-
`java.util.Properties` object, and the `freemarkerVariables` property requires a
85+
`Configuration` object (which is managed by Spring) by setting the appropriate bean
86+
properties on the `FreeMarkerConfigurer` bean. The `freemarkerSettings` property requires
87+
a `java.util.Properties` object, and the `freemarkerVariables` property requires a
8888
`java.util.Map`. The following example shows how to use a `FreeMarkerConfigurer`:
8989

9090
[source,java,indent=0]
@@ -114,6 +114,47 @@ the `Configuration` object.
114114

115115

116116

117+
[[webflux-view-freemarker-forms]]
118+
=== Form Handling
119+
[.small]#<<web.adoc#mvc-view-freemarker-forms, Same as in Spring MVC>>#
120+
121+
Spring provides a tag library for use in JSPs that contains, among others, a
122+
`<spring:bind/>` element. This element primarily lets forms display values from
123+
form-backing objects and show the results of failed validations from a `Validator` in the
124+
web or business tier. Spring also has support for the same functionality in FreeMarker,
125+
with additional convenience macros for generating form input elements themselves.
126+
127+
128+
[[webflux-view-bind-macros]]
129+
==== The Bind Macros
130+
[.small]#<<web.adoc#mvc-view-bind-macros, Same as in Spring MVC>>#
131+
132+
A standard set of macros are maintained within the `spring-webflux.jar` file for
133+
FreeMarker, so they are always available to a suitably configured application.
134+
135+
Some of the macros defined in the Spring templating libraries are considered internal
136+
(private), but no such scoping exists in the macro definitions, making all macros visible
137+
to calling code and user templates. The following sections concentrate only on the macros
138+
you need to directly call from within your templates. If you wish to view the macro code
139+
directly, the file is called `spring.ftl` and is in the
140+
`org.springframework.web.reactive.result.view.freemarker` package.
141+
142+
For additional details on binding support, see <<web.adoc#mvc-view-simple-binding, Simple
143+
Binding>> for Spring MVC.
144+
145+
146+
[[webflux-views-form-macros]]
147+
==== Form Macros
148+
149+
For details on Spring's form macro support for FreeMarker templates, consult the following
150+
sections of the Spring MVC documentation.
151+
152+
* <<web.adoc#mvc-views-form-macros, Input Macros>>
153+
* <<web.adoc#mvc-views-form-macros-input, Input Fields>>
154+
* <<web.adoc#mvc-views-form-macros-select, Selection Fields>>
155+
* <<web.adoc#mvc-views-form-macros-html-escaping, HTML Escaping>>
156+
157+
117158

118159
[[webflux-view-script]]
119160
== Script Views

src/docs/asciidoc/web/webmvc-view.adoc

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ See https://www.thymeleaf.org/documentation.html[Thymeleaf+Spring] for more deta
3535
[.small]#<<web-reactive.adoc#webflux-view-freemarker, Same as in Spring WebFlux>>#
3636

3737
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
3939
integration for using Spring MVC with FreeMarker templates.
4040

4141

@@ -98,8 +98,9 @@ properties, as the following example shows:
9898
----
9999

100100
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.
103104

104105

105106

@@ -108,10 +109,10 @@ of `welcome`, the resolver looks for the `/WEB-INF/freemarker/welcome.ftl` templ
108109
[.small]#<<web-reactive.adoc#webflux-views-freemarker, Same as in Spring WebFlux>>#
109110

110111
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`:
115116

116117
[source,xml,indent=0]
117118
[subs="verbatim,quotes"]
@@ -145,42 +146,43 @@ with additional convenience macros for generating form input elements themselves
145146

146147
[[mvc-view-bind-macros]]
147148
==== The Bind Macros
149+
[.small]#<<web-reactive.adoc#webflux-view-bind-macros, Same as in Spring WebFlux>>#
148150

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.
151153

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
156158
directly, the file is called `spring.ftl` and is in the
157159
`org.springframework.web.servlet.view.freemarker` package.
158160

159161

160162
[[mvc-view-simple-binding]]
161163
==== Simple Binding
162164

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
164166
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:
167169

168170
[source,xml,indent=0]
169171
[subs="verbatim,quotes"]
170172
----
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'. -->
173175
<#import "/spring.ftl" as spring/>
174176
<html>
175177
...
176178
<form action="" method="POST">
177179
Name:
178-
<@spring.bind "myModelObject.name"/>
180+
<@spring.bind "personForm.name"/>
179181
<input type="text"
180182
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 />
184186
...
185187
<input type="submit" value="submit"/>
186188
</form>
@@ -189,29 +191,29 @@ The following example shows the `personForm` view that was configured earlier:
189191
----
190192

191193
`<@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
196198
`defaultHtmlEscape` in `web.xml`.
197199

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.
203205

204206

205207
[[mvc-views-form-macros]]
206-
==== Input macros
208+
==== Input Macros
207209

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.
212214

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:
215217

216218
[[views-macros-defs-tbl]]
217219
.Table of macro definitions
@@ -263,9 +265,9 @@ parameter list that each takes:
263265
| <@spring.showErrors separator, classOrStyle/>
264266
|===
265267

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.
269271

270272
The parameters to any of the above macros have consistent meanings:
271273

@@ -290,8 +292,7 @@ The parameters to any of the above macros have consistent meanings:
290292
element that wraps each error uses. If no information is supplied (or the value is
291293
empty), the errors are wrapped in `<b></b>` tags.
292294

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.
295296

296297
[[mvc-views-form-macros-input]]
297298
===== Input Fields
@@ -300,7 +301,7 @@ The `formInput` macro takes the `path` parameter (`command.name`) and an additio
300301
parameter (which is empty in the upcoming example). The macro, along with all other form
301302
generation macros, performs an implicit Spring bind on the path parameter. The binding
302303
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.
304305

305306
The `showErrors` macro takes a separator parameter (the characters that are used to
306307
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:
333334
----
334335

335336
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
337338
information or `rows` and `cols` attributes for the `textarea`.
338339

339340
[[mvc-views-form-macros-select]]
@@ -367,7 +368,7 @@ model under the name 'cityMap'. The following listing shows the example:
367368
The preceding listing renders a line of radio buttons, one for each value in `cityMap`, and uses a
368369
separator of `""`. No additional attributes are supplied (the last parameter to the macro is
369370
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
371372
labels that the user sees. In the preceding example, given a list of three well known cities
372373
and a default value in the form backing object, the HTML resembles the following:
373374

@@ -414,7 +415,7 @@ user still sees the more user-friendly city names, as follows:
414415
[[mvc-views-form-macros-html-escaping]]
415416
==== HTML Escaping
416417

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
418419
compliant and that use the default value for HTML escaping defined in your `web.xml` file, as
419420
used by Spring's bind support. To make the elements be XHTML compliant or to override
420421
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
432433
<#assign xhtmlCompliant = true>
433434
----
434435

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.
437438

438439
In similar fashion, you can specify HTML escaping per field, as the following example shows:
439440

0 commit comments

Comments
 (0)