validationjsfomnifaceshttp-status-code-400viewparams

Send HTTP 400 error when <f:viewParam required> has failed on @NotNull property


I'm creating a JSF 2.2 page that requires a GET parameter to display data but I'm having difficult to enforce that parameter.

I also tried to use Omnifaces's viewParam without success, I don't know what to try anymore.

I also found a similar issue on this link and I tried all suggestions...

When I open:

It works correctly, Param is: foo is displayed.

But when I open:

Param is: null! is displayed instead of a validation error message. And null! is only displayed because I added a special outputText only for this test, on the real application it would cause a NullPointerException somewhere.

How do I make the required attribute on <f:viewParam /> or <o:viewParam /> work?

My test code:

required-test.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:o="http://omnifaces.org/ui">
<f:metadata>
    <f:viewParam name="test" value="#{requiredTestBean.value}" required="true" />
</f:metadata>

<h:head>
    <title>Required Test</title>
</h:head>

<h:body>
    Param is:
    <h:outputText value="#{requiredTestBean.value}" rendered="#{requiredTestBean.value != null}" />
    <h:outputText value="null!" rendered="#{requiredTestBean.value == null}" />
</h:body>
</html>

RequiredTestBean.java

package test;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.NotNull;

@ManagedBean
@RequestScoped
public class RequiredTestBean
{
    @NotNull
    private String value;

    @NotNull
    public String getValue()
    {
        return value;
    }

    public void setValue(@NotNull String value)
    {
        this.value = value;
    }
}

Context params on web.xml

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.CLIENT_WINDOW_MODE</param-name>
    <param-value>url</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>true</param-value>
</context-param>

Solution

  • It's incredible how I search for a solution for hours and I find it 20 minutes after asking this question...

    I just replaced the view param tag with:

    <o:viewParam name="test" value="#{requiredTestBean.value}" required="true">
        <o:viewParamValidationFailed sendError="400" />
    </o:viewParam>
    

    And it worked perfectly.

    Reference: https://stackoverflow.com/a/29841384/804976