JSF FacesMessage仅显示一次,但应该在每次发生错误时显示一次

问题描述 投票:0回答:1

我正在使用在WildFly 17(Ubuntu 18.04 TLS /或Windows 10)上运行的JSF 2.3(Mojarra 2.3.9.SP02)和PrimeFaces 7.0。在支持bean(它是ViewScoped)中的操作方法weiterBid()中,我检查用户是否刚刚选中了一个复选框。如果不是,则错误消息将在组件<p:panel id="errorMsgPanelId"中以红色显示(请参阅该方面下的内容),并且用户将停留在同一页面上。每次用户单击“下一步”按钮时,都会执行此检查。

现在,我的理解是,即使用户关闭错误消息面板,每次他/她单击“下一步”按钮时,都会调用该操作方法,并且检查该用户是否已选中该复选框。一次又一次地表演。如果尚未选中该复选框,则错误消息应在与以前相同的<p:panel id="errorMsgPanelId">组件中再次显示。这真的是应有的行为吗?如果是的话,那不是我有一个错误,那就是错误消息仅在第一次单击“下一步”按钮时显示,并且一旦用户关闭显示它的面板,就不再显示该错误消息。

我的后备豆:

@Named("aveBidUnterlagenBean")
@javax.faces.view.ViewScoped
public class AveBidUnterlagenBean implements Serializable {

private static final long serialVersionUID = 1L;

private boolean confirmationDocumentsGiven;
private Date confirmationDocumentsDate;

private FacesMessage errorMessageOnDocumentConfirmation;

public boolean isConfirmationDocumentsGiven() {
    return confirmationDocumentsGiven;
}

public void setConfirmationDocumentsGiven(boolean confirmationDocumentsGiven) {
    this.confirmationDocumentsGiven = confirmationDocumentsGiven;
    if (confirmationDocumentsGiven) {
        this.confirmationDocumentsDate = new Date();
    }
}

public String weiterBid() {
    if (!confirmationDocumentsGiven || confirmationDocumentsDate == null) {
        String errorMessage = "Please confirm documents for the real estate";

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
        this.errorMessageOnDocumentConfirmation = message;

        return null;
    }

    return null;
}


public FacesMessage getErrorMessageOnDocumentConfirmation() {
    return errorMessageOnDocumentConfirmation;
}

public void onCloseErrorMsgPanel(CloseEvent event) {
    this.errorMessageOnDocumentConfirmation = null;
}

}

在facelet中,我有组件

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

<h:head>
    <meta charset="UTF-8"/> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Title here...</title>    
</h:head>
    <h:outputStylesheet name="primeicons/primeicons.css" library="primefaces"/>
    <h:outputScript library="primefaces" name="jquery/jquery.js"/>
    <h:form id="persDatenForm">  
        <div class="ui-fluid">
            <div style="max-width:800px;margin:auto;">
                <p:fieldset legend="Documents" toggleable="true" toggleSpeed="500">
                        <p:panel id="errorMsgPanelId" styleClass="errorMsgPanel" closable="true" widgetVar="errorMsgPanel" rendered="#{not empty aveBidUnterlagenBean.errorMessageOnDocumentConfirmation}">
                           <p:ajax event="close" listener="#{aveBidUnterlagenBean.onCloseErrorMsgPanel}" update="@parent" />
                           <h:outputText style="color:red;" value="#{aveBidUnterlagenBean.errorMessageOnDocumentConfirmation.detail}"/>
                           <h:outputText value=" "/>
                           <p:commandLink outcome="#" style="background-color:white;" onclick="PF('errorMsgPanel').close()">
                                <i class="pi pi-times"></i>
                           </p:commandLink>
                        </p:panel>
                        <div class="ui-g-12 ui-md-12">
                            <p:outputLabel value="Documents overwiev"/>
                        </div>
                        <div class="ui-g-12 ui-md-12">
                            <p:selectBooleanCheckbox value="#{aveBidUnterlagenBean.confirmationDocumentsGiven}" itemLabel="I confirm that I read the documents"/>
                        </div>
                        <br/>
                        <br/>
                        <div class="ui-g-12">
                            <div class="ui-g-6"/>
                         <div class="ui-g-4">
                             <p:commandButton  icon = "pi pi-check" value="Next" action="#{aveBidUnterlagenBean.weiterBid}" update="@form"/>
                         </div>
                      </div>
                  </p:fieldset>
            </div>
      </div>
     </h:form>  

现在,我的问题是,如果我不选中该复选框,并且第一次单击“下一步”按钮,则将调用操作方法并显示错误消息。如果我关闭错误消息面板,然后在未选中复选框的情况下再次单击相同的“下一步”按钮,则错误消息将不再显示。

是错误还是功能?我该如何克服?

谢谢您,亲切的问候:亚历克斯

jsf primefaces
1个回答
0
投票

在执行操作后执行更新。相反,请使用actionListener:

<p:commandButton  icon = "pi pi-check" value="Next" actionListener="#{aveBidUnterlagenBean.weiterBid}" update="@form"/>

在actionListener之后但在动作之前调用更新

© www.soinside.com 2019 - 2024. All rights reserved.