0

我在视图范围内放置了一个托管 bean。这是豆:

@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:formxhtml中是不可见的,它被放置在父页面中:

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

但是,如果我customerDTO = new CustomerDTO();在末尾添加actionListener saveCustomer,输入将被重置。

是清除表格的方法吗?或者有更好的方法来实现这一点?

4

1 回答 1

0

使用按钮上的 ActionEvent 清除表单数据

public void clear(){
    customerDTO = new CustomerDTO();
    }
于 2012-12-15T11:18:43.753 回答