【问题标题】:How to clear Form fields after actionListener is fired?触发 actionListener 后如何清除表单字段?
【发布时间】:2012-12-03 05:10:00
【问题描述】:

我在视图范围内放置了一个托管 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: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>

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

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

【问题讨论】:

  • 您可以使用重置按钮清除文本或调用实体的新对象
  • @MohammodHossain 谢谢,所以要使用重置按钮,我需要在视图中添加它,然后将另一个 actionListerner 与它关联,然后我可以从该 actionListener 创建一个对象。它不是做同样的事情吗,我正在通过在saveCustomer 的末尾实例化一个新的CustomerDTO 来做吗?

标签: java forms jsf-2 actionlistener managed-bean


【解决方案1】:

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

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

【讨论】:

  • 难道不是做同样的事情吗,我在saveCustomer的末尾实例化一个新的CustomerDTO
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多