JSF重置按钮没有重置几列

时间:2013-03-12 08:34:52

标签: java jsf jsf-2

我在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()" />  

1 个答案:

答案 0 :(得分:1)

如果您实际使用的是Primefaces,则可以使用专门设计用于处理数据重置的组件<p:resetInput>,以防验证失败。从官方文档中可以看出:

  

当验证失败时,输入组件将其本地值保持在状态。 ResetInput用于清除状态中的缓存值,以便组件从支持bean模型中检索它们的值。

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

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

在这种情况下,您的观点可以重组为:

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