The CDI annotation
@Param allows you to inject, convert and validate a HTTP request parameter
in a CDI managed bean. It's basically like
<f:viewParam>, but with the major difference
that the injected HTTP request parameter is available during
@PostConstruct, allowing a much easier
way of processing without the need for a
CDI issues in EAR
Note that CDI has known issues when OmniFaces is bundled in multiple WARs in a single EAR. It's important to understand that those issues are not related to OmniFaces, but to the broken CDI spec. For an overview of those issues, please refer Known issues of OmniFaces CDI features in combination with specific application servers.
Click the links below:
The following parameters will be injected and validated as:
- "text1" is injected as
String(*) and is validated as JSF
- "text2" is injected as
Stringand is validated as JSF
- "text3" is injected as
Stringand is validated as JSR303 (Bean Validation)
- "number" is injected as
Integerwith automatic JSF integer conversion
- "date" is injected as
Dateand is converted as JSF
- "nsEntity" is injected as
NonSerializableEntityand is converted as JSF
<f:converter converterId="nonSerializableEntityConverter"/>(the example also demonstrates support for non-serializable values, which is important for CDI's passivating scopes)
Result: Validation has failed!
String is actually not injected itself, but is wrapped in a value holder class called
ParamValue. This is
due to the fact that a CDI producer cannot produce an arbitrary object and a parameterized wrapper type is needed. For OmniFaces 1.7
we plan to look into a CDI extension to work around this and directly inject the converted type.
ParamValue does have
merit on its own though, as it allows access to the raw (non-converted) value and allows non-serializable types to be injected into
a passivation capable bean. This last feature works by reconverting from the raw value after a bean is restored (changes made to the
converted instance before passivation took place will be lost though).
**) NOTE: bean validation at the moment is done against the
ParamValue instance that is injected.
In many cases this will only be of limited use. We hope to directly inject the converted type in OmniFaces 1.7 and then bean validation
will make more sense.