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