cdi

components

contexts

converters

el

eventlisteners

exceptionhandlers

facesviews

filters

functions

managedbeans

push

resourcehandlers

search

servlets

taghandlers

utils

validators

viewhandlers

-

ContextParam

Cookie

Eager

FacesConverter

FacesValidator

GraphicImageBean

Param

ViewScoped

Available since OmniFaces 1.6

The CDI annotation @Param allows you to inject, convert and validate a HTTP request or path parameter in a CDI managed bean.

For HTTP request parameters it's basically like <f:viewParam>, but with the major difference that the injected parameter is directly available during PostConstruct, allowing a much easier way of processing without the need for a <f:event type="preRenderView"> or <f:viewAction> in the view.

Usage

Request parameters

The example below injects the request parameter with name foo.

@Inject @Param
private String foo;

By default the name of the request parameter is taken from the name of the variable into which injection takes place. The name can be optionally specified via the name attribute. The example below injects the request parameter with name foo into a variable named bar.

@Inject @Param(name="foo")
private String bar;

The name attribute is only mandatory when using constructor injection as there is no information about constructor parameter names. The example below injects the request parameter with name foo as a constructor parameter.

@Inject
public Bean(@Param(name="foo") String foo) {
// ...
}

Multi-valued request parameters

Multi-valued parameters are also supported by specifying a List or array type. The support was added in OmniFaces 2.4.

Path parameters

Path parameters can be injected by specifying the pathIndex attribute representing the zero-based index of the path parameter. The support was added in OmniFaces 2.5. On an example request http://example.com/mypage/firstname.lastname, which is mapped to /mypage.xhtml, the below example injects the path parameter firstname.lastname.

@Inject @Param(pathIndex=0)
private String user;

This takes precedence over the name attribute.

Conversion and validation

Standard types for which JSF already has a build in converter like String, Long, Boolean, etc or for which there's already a converter registered via forClass, can be injected without explicitly specifying a converter.

@Inject @Param
private Long id;

Other types do need a converter. The following is an example of the injection of request parameter user following a request such as http://example.com/mypage?user=42:

Note that the converter and validator attributes can be specified in 3 ways:

A string value representing the converter/validator ID like so converter="userConverter".

An EL expression returning the converter/validator ID string like so converter="#{bean.converterId}".

An EL expression returning the concrete converter/validator instance like so converter="#{converterBean}".

In case the converted parameter value is not serializable, while the managed bean is serializable, you could inject it into a field of type ParamValue, with V the actual type of the converted parameter. Deserialization in this case works by converting from the original parameter again.

If conversion or validation fails, null is injected if the injection target is NOTParamValue. Otherwise a ParamValue instance is injected, but it will contain a null value. In both cases, the conversion and validation messages (if any) will be set in the JSF context then, and FacesContext.isValidationFailed() will return true.

"text2" is injected as String and is validated as
JSF <f:validateLength minimum="3">

"text3" is injected as List<String> and is validated as JSR303 (Bean Validation) @NotNull

"number" is injected as Integer with automatic JSF integer conversion

"date" is injected as Date and is converted as
JSF <f:convertDateTime pattern="yyyyMMdd">

"nsEntity" is injected as ParamValue<NonSerializableEntity> and 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)