【问题标题】:Blank page after validation fails in JSF pageJSF 页面中验证失败后的空白页面
【发布时间】:2015-01-24 07:22:36
【问题描述】:

我正在使用 Primefaces 并尝试使用 bean 验证,但是当验证失败而不是得到适当的消息时,我只会得到一个空白页。

这是 JSF 代码

<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>

这里是主模板文件

<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>

在 ManagedBean 上,“用户名”被注释为“@Size(min=2,max=5)”,仅用于测试。如果我在点击登录按钮后输入长度小于 2 或大于 5 的用户名,我会得到一个空白页。

任何想法为什么会发生这种情况?

谢谢。

【问题讨论】:

  • 在此处发布此页面的其余部分

标签: validation jsf jsf-2 primefaces bean-validation


【解决方案1】:

由于您只是发布了部分 xhtml 代码,我无法告诉您为什么会出现问题。

不过,我举个例子来演示如何使用 bean 验证

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>

托管豆

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>

【讨论】:

  • 非常感谢您的详细解释 - 这几乎是我已经尝试过但没有成功的方法。我用你的编辑检查了我的编辑,但在验证失败后仍然面临空白页。我已经用完整的页面代码更新了原始代码 sn-p,以协助并提供有关为什么会发生这种情况的进一步线索。
  • @Ectomorph 是否可以发布您的 managedbean 和 faces-config.xml?由于您无法使我的示例正常工作,因此我认为您的项目的某些配置不合适。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2015-06-19
  • 2012-11-09
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
相关资源
最近更新 更多