验证required = true时未处理的Faces消息

时间:2013-10-25 12:50:04

标签: jsf jsf-2 primefaces

我有一个非常简单的问题,但对于我的生活,我无法看到我出错的地方。我已经开始创建一个“重置密码”页面,其中包含电子邮件和密码输入以及commandButton。两个输入字段都是必需的,但是如果我将密码留空并单击按钮,则不会显示任何消息,并且服务器会报告警告:

  

警告:有一些未处理的FacesMessages,这意味着不是每一个   FacesMessage有机会被渲染。这些未经处理   FacesMessages是:    - 请输入新密码

这是我的页面:

<!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: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">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
        <link type="text/css" rel="stylesheet" href="/css/main.css" />
        <title>Request Password Reset</title>
    </h:head>

    <h:body>
        <p:outputPanel layout="block" id="container">

            <ui:include src="/header-branding-only.xhtml" />

            <h3>
                Reset Password
            </h3>

            <p:outputPanel id="content" layout="block">

                <p>
                    <h:outputText value="Please enter your email address, a password of 8 characters or more and click 'Reset'." />
                </p>

                <h:form id="resetPasswordForm">

                    <p:panelGrid columns="2" styleClass="invisible-middlealign">

                        <p:panelGrid columns="2" styleClass="formatted" columnClasses="tdasheader, null">

                            <h:outputText value="Email Address" />
                            <p:inputText value="#{passwordResetBean.email}" size="40" 
                                required="true" requiredMessage="Please enter your email address" />

                            <h:outputText value="New Password" />
                            <p:password value="#{passwordResetBean.password}" size="40" 
                                required="true" requiredMessage="Please enter a new password" />

                        </p:panelGrid>

                        <p:commandButton value="Send" actionListener="#{passwordResetBean.resetPassword}" />

                    </p:panelGrid>

                    <h:messages style="color:red" />
                </h:form>

            </p:outputPanel>

            <ui:include src="/footer.xhtml" />

        </p:outputPanel>

    </h:body>

</html>

和我的支持bean:

    public PasswordResetBean() throws ApplicationException {

            uuid = FacesUtil.getRequestParameter("uuid");
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

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

    public void resetPassword(ActionEvent event) {

        try {
            boolean successful = securityService.resetPassword(email, password, uuid, userId);

            if (successful) {
                FacesUtil.getHttpSession(true).setAttribute("passwordReset", true);
                FacesUtil.redirect(FacesUtil.getServletRequest().getContextPath()+"/logon.xhtml");
            }
            else {
                FacesContext context = FacesContext.getCurrentInstance();
                FacesMessage message = new FacesMessage("The reset has failed. Please try again");
                context.addMessage("resetPasswordForm", message);
            }
        }
        catch (Exception e) {
            handleException(e);
        }
    }
}

并面对配置:

<managed-bean>
    <managed-bean-name>passwordResetBean</managed-bean-name>
    <managed-bean-class>com.encounter.security.ui.PasswordResetBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
    <managed-property>
        <property-name>securityService</property-name>
        <value>#{securityService}</value>
    </managed-property>
</managed-bean>

有人可以告诉我哪里出错了吗?

谢谢, 尼尔

2 个答案:

答案 0 :(得分:1)

您需要在提交表单后更新h:messages部分,否则无法在那里显示任何消息。即将以下内容用于commandButton

<p:commandButton update="resetPasswordForm" value="Send" actionListener="#{passwordResetBean.resetPassword}" />

答案 1 :(得分:0)

PrimeFaces消息可以使用autoupdate

<p:messages id="messages" globalOnly="true" autoUpdate="true" closable="true"  />