Primefaces 消息不显示(咆哮消息也不显示)

Primefaces messages doesn't get displayed (neither growl message)

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

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

总而言之:消息事件已触发,但页面未更新。 我应该更改什么以在消息或咆哮中显示消息? 提前致谢!

嗯,问题是你没有更新 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 更新单个组件