Jsf 2.0 returns HTTP error code after failed validation of GET parameters

I have this local URL: http: // localhost: 8084 / Name / faces / Blah.xhtml? Flkatid = AAA

and this is the segment from the layout that checks the flkatid parameter and assigns it a 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, so the url is invalid. A page is displayed that reports this:

flkatid: "AAA" must be a number consisting of one or more digits. flkatid: "AAA" must be a number from -2147483648 to 2147483647. Example: 9346

JSF checks the parameter itself because it knows that the bean is an integer. A custom validator is only called if this parameter is an integer.

HTTP, . 403 (), " JSF" ( RichFaces, MyFaces, SmileyFaces - )? - f: viewParam, "" . , ?

+5
2

ExternalContext#responseSendError() . , String Integer required="true" , , JSF null/empty String Integer.

<f:metadata>
    <f:viewParam name="flkatid" value="#{floKatBean.flkatid}" 
        validator="#{floKatBean.validateFlKatId}" 
        validatorMessage="URL incomplete or invalid" 
    />
    ...
</f:metadata>

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"));
    }
}

init().

this.flkatidInteger = Integer.valueOf(this.flatkid);
+2

, ( HTTP) - f:viewParam "" preRenderView,

public void preRenderViewCheckParameters() throws IOException {
    if (!isPostback()) {
        flkatid = getFlkatidFromRequest();
        if (flkatid == null) {
            responseSendError(404, "URL incomplete or invalid!");
        }
    }
}
private Integer getFlkatidFromRequest(){
    try {
        return Integer.valueOf(getRequestParameter("flkatid"));
    } catch (NumberFormatException e) {
        return null;
    }
}

// util methods stolen from OmniFaces (check out this project, you'll love it):
// https://github.com/omnifaces/omnifaces/blob/master/src/org/omnifaces/util/Faces.java
public static String getRequestParameter(String name) {
    return FacesContext.getCurrentInstance().getExternalContext()
                       .getRequestParameterMap().get(name);
}
public static void responseSendError(int status, String message)
                           throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    facesContext.getExternalContext().responseSendError(status, message);
    facesContext.responseComplete();
}

, :

<f:metadata>
    <f:event listener="#{portariasMB.preRenderViewLoadPortaria}"
             type="preRenderView" />
</f:metadata>

, , , .

, , , , ... , .

0

All Articles