2012-12-15 89 views
0

我有一個託管bean放在視圖範圍內。這裏是bean:如何在actionListener被觸發後清除表單字段?

@ManagedBean(name = "adminController") 
@ViewScoped 
public class AdminController extends BaseWebController implements Serializable { 

    private static final long serialVersionUID = 1019716974557397635L; 

    private transient CustomerDTO customerDTO; 

    @PostConstruct 
    public void init() { 
     customerDTO = new CustomerDTO(); 
    } 

    public void saveCustomer(ActionEvent event) { 
     System.out.println(customerDTO.isActive()); 
     try { 
      getServiceProvider().getCustomerService().addNewCustomer(customerDTO); 
      getFacesContext().addMessage(null, FacesMessageUtils.getMessageForCustomerAddSuccess(getFacesContext())); 
     } catch (Throwable throwable) { 
      getFacesContext().addMessage(null, FacesMessageUtils.getMessageForCustomerAddError(getFacesContext())); 
      printStackTrace(throwable); 
     }  

    } 

    public CustomerDTO getCustomerDTO() { 
     return customerDTO; 
    } 

    public void setCustomerDTO(CustomerDTO customerDTO) { 
     this.customerDTO = customerDTO; 
    } 
} 

的觀點是:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:pe="http://primefaces.org/ui/extensions"> 

    <p:panel header="#{adbBundle['admin.addCustomerPanel.header']}" 
     id="addCustomerPanel" toggleable="true"> 
     <p:panelGrid columns="2" id="addCustomerTable" 
      styleClass="addCustomerTable"> 
      <f:facet name="header"> 
       <p:outputLabel id="header" 
        value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.header']}" /> 
      </f:facet> 

      <p:outputLabel for="customerName" 
       value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerName']}" /> 
      <h:panelGroup layout="block"> 
       <p:inputText id="customerName" styleClass="customerName" 
        autocomplete="off" 
        label="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerName']}" 
        value="#{adminController.customerDTO.customerName}" required="true" /> 
       <pe:tooltip for="customerName" 
        value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerName.tooltip']}" 
        showEffect="slideToggle" hideEffect="slideToggle" showDelay="0" 
        myPosition="left center" atPosition="right center" /> 
      </h:panelGroup> 

      <p:outputLabel for="customerId" 
       value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerId']}" /> 
      <h:panelGroup layout="block"> 
       <p:inputText id="customerId" autocomplete="off" 
        label="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerId']}" 
        value="#{adminController.customerDTO.customerId}" required="true"> 
        <f:validator validatorId="customerIDValidator" /> 
       </p:inputText> 
       <pe:tooltip for="customerId" 
        value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.customerId.tooltip']}" 
        showEffect="slideToggle" hideEffect="slideToggle" showDelay="0" 
        myPosition="left center" atPosition="right center" /> 
      </h:panelGroup> 

      <p:outputLabel for="activeStatus" 
       value="#{adbBundle['admin.addCustomerPanel.addCustomerTable.activeStatus']}" /> 
      <h:panelGroup layout="block"> 
       <p:selectBooleanCheckbox id="activeStatus" value="#{adminController.customerDTO.active}" /> 
      </h:panelGroup> 

      <f:facet name="footer"> 
       <p:commandButton value="#{adbBundle['saveButton']}" 
        actionListener="#{adminController.saveCustomer}" 
        icon="ui-icon-check" update=":growl, @form" /> 
      </f:facet> 
     </p:panelGrid> 
    </p:panel> 

</ui:composition> 

,我面對的是,後actionListener成功執行輸入字段都抱着值的問題。

h:form不是在前述的XHTML可見,它被放置在父頁爲:

<ui:composition 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:p="http://primefaces.org/ui" 
    template="/WEB-INF/templates/globalTemplate.xhtml"> 

    <ui:define name="title">#{adbBundle['home']}</ui:define> 
    <ui:define name="content"> 
     <p:growl id="growl" showDetail="true" /> 
     <h:form id="form"> 
      <ui:include src="/WEB-INF/includes/addCustomer.xhtml" /> 
     </h:form> 
    </ui:define> 

</ui:composition> 

但是如果我在actionListenersaveCustomer的末尾添加customerDTO = new CustomerDTO();,輸入被複位。

它是清除表單的方式嗎?或者有更好的方法來實現這一目標?

+0

動作事件可以使用重置按鈕清除文字或者致電實體 –

+0

@MohammodHossain感謝的新對象,因此使用一個復位按鈕,我需要將其添加在清除表單數據然後將它與另一個'actionListerner'關聯起來,然後我可以從'actionListener'創建一個對象。難道它沒有做同樣的事情嗎?我在'saveCustomer'的末尾實例化一個新的'CustomerDTO'來做什麼? –

回答

0

要使用按鈕

public void clear(){ 
    customerDTO = new CustomerDTO(); 
    } 
+0

它不是做同樣的事情,我通過在'saveCustomer'結尾處實例化一個新的'CustomerDTO'來做什麼? –