Сan I do a redirect (or error) if a f:viewParam
is empty?
<f:metadata>
<f:viewParam name="accountId" value="#{accountMB.id}"/>
</f:metadata>
When I add required="true"
, nothing happens. What are the options?
Сan I do a redirect (or error) if a f:viewParam
is empty?
<f:metadata>
<f:viewParam name="accountId" value="#{accountMB.id}"/>
</f:metadata>
When I add required="true"
, nothing happens. What are the options?
You need
<h:message(s)>
to show faces messages associated with a given (input) component. You probably already know how to do that for<h:inputText>
. You can do exactly the same for<f:viewParam>
.Not directly with standard JSF validation facilities. You'd need to do the job manually in
<f:viewAction>
(you need to make sure that you don't have any validators/converters on it, otherwise it wouldn't be invoked due to a validation/conversion error; you could alternatively use<f:event type="preRenderView">
).Sending a HTTP error can be done as below (this example sends a HTTP 400 error):
If you happen to use the JSF utility library OmniFaces, then you can use the
<o:viewParamValidationFailed>
tag for the very purpose without needing additional backing bean logic.Sending a redirect on view param validation fail:
Sending a HTTP 400 error on view param validation fail:
See also:
You can add a filter to your page (Filtering requests):
And rememeber to declare your filter in web.xml file:
Also, when working with filters, I suggest to use forward than redirect.