如果组合框为空,则在页面加载后显示消息

时间:2014-08-08 14:06:38

标签: jsf

我尝试在@PostConstruct方法中填充集合,然后抛出NPE。

还尝试重定向,但我得到IllegalStateException: Response already committed

this.networks = this.getNetworks();

if (this.networks.isEmpty()) {
    JsfUtils jsfUtils = new JsfUtils();
    jsfUtils.displayMessage("WARN", "No network inserted!", "Insert a network!");
}

然后我尝试在getter方法中直接使用FacesMessage。虽然该方法正在执行,但是我没有显示该消息,因为我在控制台中看到了该消息。

public List<Network> getNetworks() {
    if (this.networks == null) {
        networks = networkBean.findWithNamedQuery("Network.findAll");
        if (networks.isEmpty()) {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Insert a network!",""));
            System.out.println(">>>>>>>>>>>> Insert a Network!");
        }
    }
    return networks;
}

使用与@PostLoad方法中第一个相同的结构也不会显示任何消息。

网页代码:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:jsf="http://xmlns.jcp.org/jsf" xmlns:p="http://primefaces.org/ui" xmlns:pt="http://xmlns.jcp.org/jsf/passthrough" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
            template="/WEB-INF/tpl/template1.xhtml">
<ui:define name="title">Add Stablishment</ui:define>
<ui:define name="header">Add Stablishment</ui:define>
<ui:define name="content">

    <h:form id="form" enctype="multipart/form-data">

        <p:growl id="growl" autoUpdate="true" showDetail="true" sticky="true" globalOnly="true" />

        <h:panelGrid columns="3" cellpadding="5">

            <p:outputLabel for="categories" value="Category:" />
            <p:selectOneMenu id="categories" value="#{stablishmentMB.category.id}">
                <f:selectItem itemValue="" itemLabel="-- Select --" noSelectionOption="true" />
                <f:selectItems value="#{stablishmentCategoryMB.categories}" var="category" itemLabel="#{category.name}" itemValue="#{category.id}" />
            </p:selectOneMenu>
            <p:message for="categories" />

            <p:outputLabel for="networks" value="Network:" />
            <p:selectOneMenu id="networks" value="#{stablishmentMB.network.id}">
                <f:selectItem itemValue="" itemLabel="-- Selecione --" noSelectionOption="true" />
                <f:selectItems value="#{networkMB.networks}" var="network" itemLabel="#{network.tradeName}" itemValue="#{network.id}" />
            </p:selectOneMenu>
            <p:message for="networks" />

            <p:outputLabel for="cnpj" value="CNPJ:" />
            <p:inputMask id="cnpj" pt:placeholder="99.999.999/9999-99" mask="99.999.999/9999-99" value="#{stablishmentMB.stablishment.cnpj}" required="true" />
            <p:message for="cnpj" />

            <p:outputLabel for="companyName" value="Company Name:" />
            <p:inputText id="companyName" value="#{stablishmentMB.stablishment.companyName}" required="true" />
            <p:message for="companyName" />

            <p:outputLabel for="tradeName" value="Trade Name:" />
            <p:inputText id="tradeName" value="#{stablishmentMB.stablishment.tradeName}" required="true" />
            <p:message for="tradeName" />

            <p:outputLabel for="zipCode" value="Zip Code:" />
            <p:inputMask id="zipCode" mask="99999-999" value="#{stablishmentMB.zipCode.number}" required="true">
                <p:ajax event="blur" listener="#{stablishmentMB.findByZipCode}" update="street neighbourhood city state" />
            </p:inputMask>
            <p:message for="zipCode" />

            <p:outputLabel for="state" value="State:" />
            <p:inputText id="state" value="#{stablishmentMB.state.abbreviation}" required="true" />
            <p:message for="state" />

            <p:outputLabel for="city" value="City:" />
            <p:inputText id="city" value="#{stablishmentMB.city.nome}" required="true" />
            <p:message for="city" />

            <p:outputLabel for="neighbourhood" value="Neighbourhood:" />
            <p:inputText id="neighbourhood" value="#{stablishmentMB.neighbourhood.nome}" />
            <p:message for="neighbourhood" />

            <p:outputLabel for="street" value="Street:" />
            <p:inputText id="street" value="#{stablishmentMB.street.name}" required="true" />
            <p:message for="street" />

            <p:outputLabel for="number" value="Nº:" />
            <p:inputText id="number" value="#{stablishmentMB.stablishment.number}" required="true" />
            <p:message for="number" />

            <p:outputLabel for="complement" value="Complement:" />
            <p:inputText id="complement" value="#{stablishmentMB.stablishment.complement}" />
            <p:message for="complement" />

            <p:outputLabel for="telephone" value="Telephone:" />
            <p:inputMask mask="(99) 9999-9999" id="telephone" value="#{stablishmentMB.stablishment.telephone}" />
            <p:message for="telephone" />

            <p:outputLabel for="contact" value="Contact:" />
            <p:inputText id="contact" value="#{stablishmentMB.stablishment.contact}" />
            <p:message for="contact" />

            <p:outputLabel for="email" value="E-mail:" />
            <p:inputText id="email" value="#{stablishmentMB.stablishment.email}" />
            <p:message for="email" />

            <p:outputLabel for="site" value="Site:" />
            <p:inputText id="site" value="#{stablishmentMB.stablishment.site}" />
            <p:message for="site" />

            <p:outputLabel for="latitude" value="Latitude:" />
            <p:inputText id="latitude" value="#{stablishmentMB.stablishment.latitude}" />
            <p:message for="latitude" />

            <p:outputLabel for="longitude" value="Longitude:" />
            <p:inputText id="longitude" value="#{stablishmentMB.stablishment.longitude}" />
            <p:message for="longitude" />

            <p:commandButton action="#{stablishmentMB.save}" value="Save" update="@form" />

        </h:panelGrid>

    </h:form>

</ui:define>

1 个答案:

答案 0 :(得分:0)

你应该从getter中删除你的代码,因为getter并不意味着要做这样的工作人员,并且总是在你的bean上任何getter之前执行@PostConstruct!

快速解决问题的方法是将<p:growl>移到页面末尾,这将使渲染器尝试渲染<p:growl>.

之前进行bean构建