Skip to content

Commit c34b199

Browse files
committed
Documentation for custom converters
1 parent 7d90ff5 commit c34b199

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎docs/src/main/docbook/en-US/Configuration.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="config/Dynamic_views.xml" />
1818
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="config/Page_actions.xml" />
1919
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="config/Validation.xml" />
20+
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="config/Conversion.xml" />
2021
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="config/Annotations.xml" />
2122

2223
</chapter>

‎docs/src/main/docbook/en-US/config/Annotations.xml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,55 @@ public class CategoryBean {
348348
</section>
349349

350350

351+
<!-- ========================================================== -->
352+
<!-- Parameters Conversion -->
353+
<!-- ========================================================== -->
354+
355+
<section id="config.annotations.conversion">
356+
357+
<title>Parameter conversion</title>
358+
359+
<para>
360+
The configuration of parameter converters is very simple when using
361+
PrettyFaces annotations. To specify a custom converter for a query parameter,
362+
just add a <code>@URLConverter</code> annotation with a JSF converter ID
363+
to the field.
364+
</para>
365+
366+
<programlisting><![CDATA[@Named("bean")
367+
@RequestScoped
368+
public class LanguageBean {
369+
370+
@URLQueryParameter("language")
371+
@URLConverter(converterId="languageConverter")
372+
private Locale language;
373+
374+
/* Getters + Setters */
375+
}]]></programlisting>
376+
377+
<para>
378+
To specify custom converters for path parameters, you have to add the
379+
<code>@URLConverter</code> to the <code>@URLMapping</code> and specify the
380+
index of the path parameter you are referring to.
381+
</para>
382+
383+
<programlisting><![CDATA[@Named("bean")
384+
@RequestScoped
385+
@URLMapping(id = "viewItem", pattern = "/store/item/#{ bean.itemId }/", viewId = "/faces/shop/item.jsf",
386+
converter=@URLConverter(index=0, converterId="ItemIdConverter"))
387+
public class CategoryBean {
388+
/* some code */
389+
}
390+
]]></programlisting>
391+
392+
<para>
393+
This will tell PrettyFaces to convert the first path parameter
394+
<code>#{bean.itemId}</code> with the converter <code>ItemIdConverter</code>.
395+
</para>
396+
397+
</section>
398+
399+
351400
<!-- ========================================================== -->
352401
<!-- Bean name resolving -->
353402
<!-- ========================================================== -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3+
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
4+
5+
<section id="config.conversion">
6+
7+
<title>* Parameter conversion</title>
8+
9+
<para>
10+
PrettyFaces supports automatic conversion of path and query parameters into common
11+
Java types. Additionally it is possible to register custom converters for individual
12+
parameters. The configuration required to attach a custom converter differs between
13+
path and query parameters.
14+
</para>
15+
16+
<section id="config.conversion.queryparam">
17+
18+
<title>Query parameters</title>
19+
20+
<para>
21+
To attach a custom JSF converter to a query parameter, just add the <code>converterId</code>
22+
attribute to the parameter configuration.
23+
</para>
24+
25+
<programlisting><![CDATA[<query-param name="lang" converterId="languageConverter"> #{bean.language} </query-param>]]></programlisting>
26+
27+
<para>
28+
Please not that the <code>converterId</code> must refer to a valid JSF converter ID.
29+
The following listing shows an example for a converter that is able to convert
30+
ISO 639 codes into Java <code>Locale</code> objects.
31+
</para>
32+
33+
<programlisting><![CDATA[@FacesConverter("languageConverter")
34+
public class LanguageConverter implements Converter
35+
{
36+
37+
@Override
38+
public Object getAsObject(FacesContext context, UIComponent component, String value)
39+
{
40+
return new Locale(value);
41+
}
42+
43+
@Override
44+
public String getAsString(FacesContext context, UIComponent component, Object value)
45+
{
46+
return ((Locale) value).getLanguage();
47+
}
48+
49+
}
50+
]]></programlisting>
51+
52+
</section>
53+
54+
<section id="config.conversion.pathparam">
55+
56+
<title>Path parameters</title>
57+
58+
<para>
59+
Conversion of path parameters is very similar to the conversion of query parameters.
60+
The only important difference is that you have to specify the <code>index</code> of
61+
the path parameter you want to convert. The <code>index</code> is the absolute position
62+
of the path parameter in the pattern. The first parameter is addressed with <code>0</code>.
63+
</para>
64+
65+
<programlisting><![CDATA[<url-mapping id="viewItem">
66+
<pattern value="/store/item/#{ bean.itemId }/">
67+
<convert index="0" converterId="ItemIdConverter" />
68+
</pattern>
69+
<view-id value="/faces/shop/item.jsf" />
70+
</url-mapping>]]></programlisting>
71+
72+
</section>
73+
74+
</section>

0 commit comments

Comments
 (0)