【问题标题】:How to create a empty page in JSF app with Netbeans and Primefaces?如何使用 Netbeans 和 Primefaces 在 JSF 应用程序中创建一个空白页面?
【发布时间】:2018-05-01 18:11:24
【问题描述】:

我有一个由 JSF 使用 Netbeans 中的 Primefaces 模板创建的应用程序。 其中一个页面是 Create.xhtml,用于在 mysql 数据库中插入新记录。此页面由列表 od 记录页脚中的按钮调用。 当我选择这个页面时,表单是空的,只有标题和按钮的问题。 outputLabel 和 inputText 是“隐藏的”(在浏览器中检查页面)。但是,如果我首先选择列表中的一条记录,然后选择创建页面,则 outputLabel 和 inputText 已经可见,但带有值。 我希望当我选择创建页面时,这是一个空表单,当我想在数据库中插入我想要的值时。 List.xhtml的代码:

<h:form id="DistritoCreateForm">
    <p:panel header="#{bundle.CreateDistritoTitle}">
    <h:panelGroup id="display">
        <p:panelGrid columns="2" rendered="#{distritoController.selected != null}">
            <p:outputLabel value="#{bundle.CreateDistritoLabel_nomeDistrito}" for="nomeDistrito" />
            <p:inputText id="nomeDistrito" value="#{distritoController.selected.nomeDistrito}" title="#{bundle.CreateDistritoTitle_nomeDistrito}" required = "true" />
        </p:panelGrid>
        <p:commandButton actionListener="#{distritoController.create}" value="#{bundle.Save}" update="display,:growl" oncomplete="handleSubmit(args,'DistritoCreateForm');" action="List.xhtml"/>
        <p:commandButton value="#{bundle.Cancel}" action="List.xhtml"/>
    </h:panelGroup>
    </p:panel>
</h:form>

Create.xhtml的代码:

<h:form id="DistritoListForm">
    <p:panel header="#{bundle.ListDistritoTitle}">
        <p:dataTable id="datalist" value="#{distritoController.items}" var="item" style="width:50%;"
                     selectionMode="single" selection="#{distritoController.selected}"
                     paginator="true"
                     paginatorPosition="bottom"
                     rowKey="#{item.id}"
                     rows="10">

            <p:ajax event="rowSelect"   update="createButton viewButton editButton deleteButton"/>

            <p:column filterBy="#{item.nomeDistrito}" filterMatchMode="contains">
                <f:facet name="header">
                    <h:outputText value="#{bundle.ListDistritoTitle_nomeDistrito}"/>
                </f:facet>
                <h:outputText value="#{item.nomeDistrito}"/>
            </p:column>
            <f:facet name="footer">
                <p:commandButton id="createButton" icon="ui-icon-plus" actionListener="#{distritoController.prepareCreate}" action="Create.xhtml"/>
                <p:commandButton id="viewButton"   icon="ui-icon-search" action="View.xhtml"/>
                <p:commandButton id="editButton"   icon="ui-icon-pencil" action="Edit.xhtml"/>
                <p:commandButton id="deleteButton" icon="ui-icon-trash" actionListener="#{distritoController.destroy}" update=":growl,datalist" disabled="#{empty distritoController.selected}"/>
            </f:facet>
        </p:dataTable>
    </p:panel>
</h:form>

【问题讨论】:

    标签: jsf primefaces xhtml


    【解决方案1】:

    如此有效(在您的代码中无法看到,因为它不是mcve

    • 你选择了一些东西
    • 因此,某个变量被设置为在共享的“显示”/“编辑”/“创建”页面/对话框中使用
    • 接下来,您按下创建按钮,将显示相同的页面/对话框,其中包含之前选择的内容。

    那么一个简单的推论是在按下创建按钮时调用的方法中没有清除'selected'变量(#{distritoController.prepareCreate}

    解决方案:

    清除变量(例如,通过创建对象的新空实例...

    实际上,这不是 PrimeFaces 问题(纯 jsf 也会发生同样的情况),它也不是“java”问题,而是纯编码问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2017-04-12
      • 2016-12-31
      • 2012-03-19
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多