0

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:

  • http://url-to/required-test.xhtml?test=foo

It works correctly, Param is: foo is displayed.

But when I open:

  • http://url-to/required-test.xhtml?test=
  • http://url-to/required-test.xhtml

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>
Community
  • 1
  • 1
  • Your question is confusing. Where is the `` which should show the validation error message? – BalusC Apr 30 '16 at 05:59
  • @BalusC I already resolved the problem with your answer on an other question, thanks ;P . I didn't know that the validation error was going to be displayed on `` when I asked, I was expecting the server to show a `HTTP 400 Bad Request` error page informing the validation error. `` solved that. – José Roberto Araújo Júnior Apr 30 '16 at 15:27
  • Ah ok, the initial question didn't make your expectations clear. – BalusC Apr 30 '16 at 15:40

1 Answers1

1

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

Community
  • 1
  • 1