0
votes

I'm using Primefaces and trying to use bean validation but when validation fails instead of getting an appropriate message I just get a blank pages.

Heres the JSF code

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui">

<ui:composition template="/WEB-INF/xhtml/templates/main.xhtml">
    <ui:param name="header" value="small-header" />

    <ui:define name="content">

        <div style="margin-top: 60px;"></div>
        <h1>Member Sign In</h1>

        <h:form>
            <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />

            <h:panelGrid columns="3">  

                <h:outputLabel value="Username : "/>
                <p:inputText id="username" value="#{signInView.username}"/>
                <p:message for="username" style="color:red" />

                <h:outputLabel value="Password :"/>
                <p:password id="password" value="#{signInView.password}"/>
                <p:message for="password" style="color:red" />

            </h:panelGrid>
            <p:commandButton style="margin-top: 20px;" 
                             value="Sign In" 
                             action="#{signInView.signIn}"  
                             validateClient="false" 
                             ajax="false"/>
        </h:form> 

        <h:outputText>By signing in you agree to abide by our terms and conditions.</h:outputText>

        <div style="margin-top: 50px;"></div>
        <h:outputText>Not currently a member ?
            <p:link outcome="/content/join/join" value="sign up for free"></p:link>
        </h:outputText>

        <div style="margin-top: 150px;"></div>

    </ui:define>

</ui:composition>

And here is the main template file

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:c="http://java.sun.com/jsp/jstl/core">

<h:head>
    <f:facet name="first">
        <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
        <meta http-equiv="pragma" content="no-cache" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="0" />

        <h:outputStylesheet library="css" name="style.css"  />
        <h:outputScript library="js" name="menu.js" />
        <link href='http://fonts.googleapis.com/css?family=Roboto:300,300italic' rel='stylesheet' type='text/css'/>

        <title>
            <ui:insert name="title">TODO: Add title here</ui:insert>
        </title>

    </f:facet>        
</h:head>

<h:body>

    <ui:include src="/WEB-INF/xhtml/includes/main/title_banner.xhtml">
        <ui:param name="header" value="#{header}" />
    </ui:include>

    <ui:include src="/WEB-INF/xhtml/includes/menu/main_menu.xhtml"/>

    <div class='white-bg'>

        <div id="content">
            <ui:insert name="content">TODO: Add content here</ui:insert>
        </div>

    </div>

    <ui:include src="/WEB-INF/xhtml/includes/main/footer.xhtml" />


</h:body>

And on the ManagedBean 'username' is annotated with '@Size(min=2,max=5)' just for testing. If I enter a username of length smaller than 2 or larger than 5 after hitting the Sign In button I get a blank page.

Any ideas why this is happening?

Thanks.

1
Post the rest of this page herekolossus

1 Answers

0
votes

Since you just post partial xhtml code, I cannot suggest you why problem occurs.

However, I give an example to demonstrate how to use bean validation

login.xhtml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">


    <f:view contentType="text/html" encoding="UTF-8">

        <!-- ################################################################# -->
        <!-- ## Header -->
        <h:head>
            <f:facet name="first">
                <meta http-equiv="Content-Type" 
                      content="text/html; charset=UTF-8" />
                <meta http-equiv="pragma" content="no-cache" />
                <meta http-equiv="cache-control" content="no-cache" />
                <meta http-equiv="expires" content="0" />
            </f:facet>
        </h:head>

        <h:body>
            <h:form>
                <p:messages id="messages" 
                            showDetail="true" 
                            autoUpdate="true" 
                            closable="true" />
                <h:panelGrid columns="3">  

                    <h:outputLabel value="Username : "/>
                    <p:inputText id="username" value="#{signInView.username}"/>
                    <p:message for="username" style="color:red" />

                    <h:outputLabel value="Password :"/>
                    <p:password id="password" value="#{signInView.password}"/>
                    <p:message for="password" style="color:red" />

                </h:panelGrid>
                <p:commandButton style="margin-top: 20px;" 
                                 value="Sign In" 
                                 action="#{signInView.signIn}"  
                                 validateClient="false" 
                                 ajax="false"/>
            </h:form> 
        </h:body>

    </f:view>

</html>

menu.xhtml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">


    <f:view contentType="text/html" encoding="UTF-8">

        <!-- ################################################################# -->
        <!-- ## Header -->
        <h:head>
            <f:facet name="first">
                <meta http-equiv="Content-Type" 
                      content="text/html; charset=UTF-8" />
                <meta http-equiv="pragma" content="no-cache" />
                <meta http-equiv="cache-control" content="no-cache" />
                <meta http-equiv="expires" content="0" />
            </f:facet>
        </h:head>

        <h:body>
            Menu page
        </h:body>

    </f:view>

</html>

managedbean

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.Size;

/**
 *
 * @author Wittakarn
 */
@SessionScoped
@ManagedBean(name = "signInView")
public class SignInView implements Serializable{

    @Size(min=2,max=5)
    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String signIn(){
        System.out.println("username = " + username);
        return "/menu.xhtml";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         version="3.0">
    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>