gpt4 book ai didi

java - JSF 重置按钮未重置几列

转载 作者:行者123 更新时间:2023-12-01 14:46:51 24 4
gpt4 key购买 nike

我在 JSF 页面中有以下内容,当我按下重置按钮时,除了 employeenumber(empnumber)phone(phoneno) 之外的所有值都会重置,甚至 autoComplete(empname) 被重置。这可能是什么原因?

    <p:panelGrid   columns="6"> 

<h:outputLabel value="Employee Name" />
<p:autoComplete value="#{myMB.employee}" id="empname"
completeMethod="#{myMB.complete}" converter="myConverter"
var="p" itemLabel="#{p.employeeName}" itemValue="#{p}"
forceSelection="true" >
<p:ajax event="itemSelect" listener="#{myMB.handleSelect}"
update ="empnumber phoneno" />
<p:column>#{p.employeeName} - #{p.employeeNumber}</p:column>
</p:autoComplete>

<p:message id="badgeMessage" for="empname" />

<h:outputLabel value="Employee Number" />
<p:inputText id="empnumber" value="#{myMB.employeeDetail.employeeNumber}" >
</p:inputText>
<p:message id="empnumMessage" for="empnumber" />

<h:outputLabel value="Phone #" for="phoneno" />
<p:inputText id="phoneno" value="#{myMB.employeeDetail.phoneNo}">
</p:inputText>
<p:message id="phoneMessage" for="phoneno" />

</p:panelGrid>
<p:separator />
<p:panelGrid >
<h:outputLabel value="Department" for="department" />
<h:selectOneMenu id="department" value="#{myMB.department}"
converter="departmentConverter">
<f:selectItems value="#{myMB.departmentItems}" var="dept"
itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
<p:ajax listener="#{myMB.loadDepartments}" />
</h:selectOneMenu>
<p:message id="categoryMessage" for= "department" />


<p:commandButton type="reset" value="Cancel" icon="ui-icon-close"
onclick="myDialog.hide()" />

最佳答案

如果您实际上正在使用 Primefaces,那么使用其组件 <p:resetInput> 是有意义的。这是专门设计用于在验证失败时处理数据重置的。从官方文档可以看出:

Input components keep their local values at state when validation fails. ResetInput is used to clear the cached values from state so that components retrieve their values from the backing bean model instead.

如果验证失败,组件的工作方式如下,组件的值在 target 中指定。 <p:resetInput> 的属性,当附加标签时,将重新填充初始值。 <p:resetInput>可以附加到操作源组件,例如 <p:commandButton> .

为了简化其使用,您可以选择将所有要重置的组件分组到一个容器中,例如 <p:panel id="panel"> ,正如您从展示示例中看到的那样。

从这个角度来看,您的 View 可以重组为:

<h:form>
<p:panel id="panel">
<p:panelGrid columns="6">
<h:outputLabel value="Employee Name" />
<p:autoComplete value="#{myMB.employee}" id="empname"
completeMethod="#{myMB.complete}" converter="myConverter"
var="p" itemLabel="#{p.employeeName}" itemValue="#{p}"
forceSelection="true" >
<p:ajax event="itemSelect" listener="#{myMB.handleSelect}"
update ="empnumber phoneno" />
<p:column>#{p.employeeName} - #{p.employeeNumber}</p:column>
</p:autoComplete>
<p:message id="badgeMessage" for="empname" />
<h:outputLabel value="Employee Number" />
<p:inputText id="empnumber" value="#{myMB.employeeDetail.employeeNumber}" >
</p:inputText>
<p:message id="empnumMessage" for="empnumber" />
<h:outputLabel value="Phone #" for="phoneno" />
<p:inputText id="phoneno" value="#{myMB.employeeDetail.phoneNo}">
</p:inputText>
<p:message id="phoneMessage" for="phoneno" />
</p:panelGrid>
<p:separator />
<h:outputLabel value="Department" for="department" />
<h:selectOneMenu id="department" value="#{myMB.department}"
converter="departmentConverter">
<f:selectItems value="#{myMB.departmentItems}" var="dept"
itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
<p:ajax listener="#{myMB.loadDepartments}" />
</h:selectOneMenu>
<p:message id="categoryMessage" for= "department" />
</p:panel>

<p:commandButton value="Reset input values" update="panel" process="@this" onclick="myDialog.hide()" icon="ui-icon-close">
<p:resetInput target="panel"/>
</p:commandButton>
</h:form>

关于java - JSF 重置按钮未重置几列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com