@Qualifier @Retention(value=RUNTIME) @Target(value={TYPE,METHOD,FIELD,PARAMETER}) public @interface Param
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 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.
By default the name of the request parameter is taken from the name of the variable into which injection takes place.
The example below injects the request parameter with name foo
.
@Inject @Param private String foo;
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;
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
:
@Inject @Param(converter="userConverter", validator="priviledgedUser") private User user;
Note that the converter
and validator
attributes can be specified in 3 ways:
converter="userConverter"
.
converter="#{bean.converterId}"
.
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 request parameter.
Deserialization in this case works by converting from the original request parameter again.
@Inject @Param(converter="someIdToInputStreamConverter") private ParamValue<InputStream> content; // Extreme example :) Be careful with resource leaking.
If conversion or validation fails, null
is injected if the injection target is NOT
ParamValue
. 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
.
ParamValue
,
Attribute
,
ParamExtension
,
RequestParameterProducer
,
DynamicParamValueProducer
Modifier and Type | Optional Element and Description |
---|---|
String |
converter
(Optional/Required) The converter to be used for converting the request parameter to the type that is to be injected.
|
Attribute[] |
converterAttributes
(Optional) Attributes that will be set on the converter instance obtained from
converter() or converterClass() . |
Class<? extends Converter> |
converterClass
(Optional) Class of the converter to be used for converting the request parameter to the type that is to be injected.
|
String |
converterMessage
(Optional) A message that will be used if conversion fails instead of the message set by the converter.
|
boolean |
disableBeanValidation
(Optional) Flag that disables bean validation for this instance.
|
String |
label
(Optional) the label used to refer to the request parameter.
|
String |
name
(Optional) The name of the request parameter.
|
boolean |
overrideGlobalBeanValidationDisabled
(Optional) Flag that overrides the global
BeanValidator.DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME setting. |
boolean |
required
(Optional) Flag indicating if this request parameter is required (must be present) or not.
|
String |
requiredMessage
(Optional) A message that will be used if a non-empty value is submitted instead of the default message associated
with the
RequiredValidator . |
Attribute[] |
validatorAttributes
(Optional) Attributes that will be set on each of the validator instances obtained from
validators() and validatorClasses() . |
Class<? extends Validator>[] |
validatorClasses
(Optional) Class of one ore more validators to be used for validating the (converted) request parameter.
|
String |
validatorMessage
(Optional) A message that will be used if validation fails instead of the message set by the validator(s).
|
String[] |
validators
(Optional) The validators to be used for validating the (converted) request parameter.
|
public abstract String name
public abstract String label
public abstract String converter
A converter can be specified in 3 ways:
Application.createConverter(String)
Converter
instance.
If this attribute is specified in addition to converterClass()
, this attribute takes precedence.
public abstract boolean required
Utils.isEmpty(Object)
.public abstract String[] validators
A validator can be specified in 3 ways:
Application.createValidator(String)
Validator
instance.
If this attribute is specified in addition to validatorClasses()
then the validators from both
attributes will be added to the final collection of validators. The validators from this attribute will however
be called first.
public abstract Class<? extends Converter> converterClass
converter()
is specified.public abstract Class<? extends Validator>[] validatorClasses
validators()
.public abstract Attribute[] converterAttributes
converter()
or converterClass()
.
For each attribute the converter instance should have a writable JavaBeans property with the same name. The value can be a string literal
or an EL expression. String literals are coerced if necessary if there's a PropertyEditor
available (the JDK provides these for
the primitive types and their corresponding boxed types).
Attributes for which the converter doesn't have a property (setter) are silently ignored.
public abstract Attribute[] validatorAttributes
validators()
and validatorClasses()
.
For each attribute the validator instances should have a writable JavaBeans property with the same name. The value can be a string literal
or an EL expression. String literals are coerced if necessary if there's a PropertyEditor
available (the JDK provides these for
the primitive types and their corresponding boxed types).
Attributes for which any given validator doesn't have a property (setter) are silently ignored.
public abstract String converterMessage
The value for which conversion failed is available as {0}
. The label associated with this
parameter value (see the label()
attribute) is available as {1}
.
converter()
or converterClass()
fail.public abstract String validatorMessage
The value for which validation failed is available as {0}
. The label associated with this
parameter value (see the label()
attribute) is available as {1}
.
validators()
or validatorClasses()
fail.public abstract String requiredMessage
RequiredValidator
.
The (empty) value for which the required check failed is available as {0}
. (this will be either null or the empty string)
The label associated with this parameter value (see the label()
attribute) is available as {1}
.
required()
is true
.public abstract boolean disableBeanValidation
NOTE: bean validation at the moment (OmniFaces 1.6) is done against the ParamValue
that is injected. In many cases this will
be of limited use. We hope to directly inject the converted type in OmniFaces 1.7 and then bean validation will make more sense.
If true
no bean validation will be attempted. If false
(the default) no specific action is taken, and it
will depend on the availability of bean validation and the global BeanValidator.DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
setting
whether bean validation is attempted or not.
public abstract boolean overrideGlobalBeanValidationDisabled
BeanValidator.DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
setting.
If true
bean validation will be performed for this instance (given that bean validation is available) despite
it globally being disabled. If false
(the default) no specific action is taken.
Copyright © 2012–2015 OmniFaces. All rights reserved.