Primefaces消息不会显示(两声咆哮消息)

时间:2019-12-27 15:07:42

标签: jsf primefaces wildfly java-ee-7

我有一个必须填写的表格,用户才能继续。因此,在正确填写每个字段之前,该按钮将被禁用。为了帮助用户,我想在未正确填写字段(例如电子邮件地址)时显示消息。 问题是未显示该消息。我尝试使用Primefaces的消息,消息,咆哮而没有任何结果。我还尝试使用javax验证约束,并且日志显示该消息已被触发,但未显示。 页面如下:

<?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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
</h:head>
<h:body>
    <h:outputLabel value="Test registration"/>
    <h:form id="registration-form">
        <p:growl showDetail="true"/>
        <h:panelGrid id="panel" columns="3">
            <p:outputLabel for="@next" value="Your Name: "/>
            <p:inputText id="name-input" value="#{testBean.name}" label="Name">
                <p:ajax update="save" event="blur" listener="#{testBean.validateName}"/>
            </p:inputText>
            <p:messages id="name-input-message" for="@previous" showDetail="true">
            </p:messages>

            <p:outputLabel for="@next" value="Email: "/>
            <p:inputText id="email-input" value="#{testBean.email}" label="Email">
                <p:ajax update="save" event="blur" listener="#{testBean.validateEmail}"/>
            </p:inputText>
            <p:messages id="email-input-message" for="@previous" showDetail="true"/>

            <p:outputLabel for="@next" value="Password: "/>
            <p:inputText id="password-1-input" value="#{testBean.password1}" label="Password1">
                <p:ajax update="save" event="blur" listener="#{testBean.checkPasswords}"/>
            </p:inputText>
            <p:messages id="password-1-message" for="@previous" showDetail="true"/>

            <p:outputLabel for="@next" value="Password again: "/>
            <p:inputText id="password-2-input" value="#{testBean.password2}" label="Password2">
                <p:ajax update="save" event="keyup" listener="#{testBean.checkPasswords}"/>
            </p:inputText>
            <p:messages id="password-2-message" for="@previous" showDetail="true"/>
        </h:panelGrid>
        <p:commandButton id="save" value="Save" disabled="#{testBean.shouldBeDisabled}" icon="fa fa-save">
        </p:commandButton>
    </h:form>
</h:body>
</html>

这个豆是:

import org.jboss.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

@ManagedBean
@ViewScoped
public class TestBean {

    private String name;
    private String email;
    private String password1;
    private String password2;

    @Inject
    Logger logger;

    // .. getters, setters


    public Boolean getShouldBeDisabled() {
        return !(isNameValid() && isEmailValid() && arePasswordsEqual());
    }

    private boolean arePasswordsEqual() {
        if (password1 == null || password2 == null) {
            return false;
        }
        else
            return password1.equals(password2);
    }

    private boolean isEmailValid() {
        boolean result = false;     
        if (email == null || email.isEmpty()) return result;
        try {
            InternetAddress address = InternetAddress.parse(email, true)[0];
            address.validate();
            result = true;
        } catch (AddressException e) {
            logger.debug("email is not valid");
        }
        return result;
    }

    private boolean isNameValid() {
        return !(name == null || name.isEmpty()) && name.length() > 1 && name.length() <= 60;
    }

    public void validateName() {
        if (!isNameValid()) {
            FacesContext.getCurrentInstance()
                    .addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"invalid","name is not valid"));
        }
    }

    public void validateEmail(){
        if(!isEmailValid()){
            FacesContext.getCurrentInstance()
                    .addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"invalid","email is not valid"));
        }
    }

    public void checkPasswords(){
        if(!arePasswordsEqual())
        FacesContext.getCurrentInstance()
                .addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"invalid","passwords must be equal"));
    }
}

记录器消息:

FINE  [javax.enterprise.resource.webcontainer.jsf.context] (default task-148) Adding Message[sourceId=<<NONE>>,summary=invalid)

总结:触发了消息事件,但是页面未更新。 我应该更改为在消息或咆哮中显示消息吗? 预先感谢!

1 个答案:

答案 0 :(得分:1)

问题是您没有更新p:messages

尝试这样的事情:

<p:inputText id="name-input" value="#{testBean.name}" label="Name">
        <p:ajax update="save name-input-message" event="blur" listener="#{testBean.validateName}"/>
        </p:inputText>
<p:messages id="name-input-message" for="@previous" showDetail="true"/>

因此,始终将整个表单更新为update="@form"或使用其ID更新单个组件