I have this local URL: http://localhost:8084/Name/faces/Blah.xhtml?flkatid=AAA
and this is a segment from a facelet, that checks the parameter "flkatid" and assigns it to the bean:
<f:metadata>
<f:viewParam name="flkatid"
id="flkatid"
value="#{floKatBean.flkatid}"
required="true"
requiredMessage="URL incomplete"
validator="#{floKatBean.validateFlKatId}"
validatorMessage="URL incomplete or invalid"
>
</f:viewParam>
<f:event type="preRenderView" listener="#{floKatBean.init}"/>
<f:event type="preRenderView" listener="#{floBean.holFloListe}"/>
</f:metadata>
floKatBean.flkatid is an integer, thus the URL is invalid. A page is displayed, that tells this:
flkatid: 'AAA' must be a number consisting of one or more digits. flkatid: 'AAA' must be a number between -2147483648 and 2147483647 Example: 9346
JSF checks the parameter itself, because it knows that the bean element is an integer. The custom validator is only called if the parameter is an integer.
Is it possible to get the server to return a HTTP error code, e.g. 403 (forbidden) without leaving the "standard JSF world" (or switch to RichFaces, MyFaces, SmileyFaces or whatever) ? I can't find anything in f:viewParam to "fail" the page. Maybe elsewhere?
You could do a ExternalContext#responseSendError()
inside the validator. But this requires that the value is been declared as a String
instead of Integer
and that required="true"
is been removed, all to prevent JSF default validation mechanism from doing the null/empty check and the conversion from String
to Integer
.
<f:metadata>
<f:viewParam name="flkatid" value="#{floKatBean.flkatid}"
validator="#{floKatBean.validateFlKatId}"
validatorMessage="URL incomplete or invalid"
/>
...
</f:metadata>
with
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException, IOException
{
if (value == null || !((String) value).matches("\\d+")) {
context.getExternalContext().responseSendError(
403, (String) component.getAttributes().get("validatorMessage"));
}
}
You could do the conversion in the init()
method instead.
this.flkatidInteger = Integer.valueOf(this.flatkid);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With