重置表单中的所有字段

时间:2013-12-07 10:06:35

标签: jsf-2 primefaces

我正在尝试在对话框窗体中选择确认按钮后重置表单字段

<h:form id="form">  
<p:panel id="panelform">
 <h:panelGrid id="formulaire" columns="2"> 
...
</h:panelGrid>
</p:panel>
 <p:dataTable ..... </p:dataTabe>
</h:form>
<p:confirmDialog style="position: absolute; width: 50px; border-color: blue" id="deleteData"  message="Your Database Will be completely removed. Are you sure? "  
                                     appendToBody="true"                                                               
                                     header="Delete List" severity="alert" widgetVar="deleteDialog">                          
                        <h:form>     
                            <p:commandButton id="confirm" value="Confirm" actionListener="#{MB.deleteData()}" update=":form" ajax="true" oncomplete="deleteDialog.hide(); purchase.hide();" >                            
                            </p:commandButton>  
                            <p:commandButton id="cancel" value="Later" onclick="deleteDialog.hide();" type="button" />                           
                        </h:form>
                    </p:confirmDialog> 

我的会话Scoped bean中的删除数据方法是

 public String deleteData() {
    logger.log(Level.SEVERE, "*****delete Datas***** ");
    dataBusinessLocal.deletedatas(datas);
    logger.log(Level.SEVERE, "*****delete Data***** ");
    dataBusinessLocal.deleteData(data);
    datas.clear();
    RequestContext.getCurrentInstance().reset("form:panelform");  
    return "datasList";
}

2 个答案:

答案 0 :(得分:1)

RequestContext.getCurrentInstance().reset("form:panelform");清除缓存的值 - 如果ValidationPhase期间的验证无法正常运行,则可以在下一页向用户显示错误的值。

我猜,它没有做的是清除模型中的值。由此引起,在RenderResponse期间,Model值将被写回xhtml输出。为了防止这种情况,只需在操作方法中手动将输入字段设置为空。

以下链接"How to clear all input fields in p:dataTable?"建议设置输入type="reset"。如果它有效,请给我一个提示。 : - )

答案 1 :(得分:0)

这是清除所有子组件的方法=重置所有值。这适用于提交的表单,显示验证错误以及表单中新输入的值。

对于JSF2,你必须使用getFacetsAndChildren()来进入Facelets。

public static void clearComponent() {
        UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();

        // for JSF 2 getFacetsAndChildren instead of only JSF 1 getChildren
        Iterator<UIComponent> children = root.getFacetsAndChildren();
        clearAllComponentInChilds(children);
}

private static void clearAllComponentInChilds(Iterator<UIComponent> childrenIt) {

        while(childrenIt.hasNext()) {
            UIComponent component = childrenIt.next();
            log.debug("handling component " + component.getId() );
            if (component instanceof HtmlInputText) {
                HtmlInputText com = (HtmlInputText) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectOneMenu) {
                HtmlSelectOneMenu com = (HtmlSelectOneMenu) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectBooleanCheckbox) {
                HtmlSelectBooleanCheckbox com = (HtmlSelectBooleanCheckbox) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectManyCheckbox) {
                HtmlSelectManyCheckbox com = (HtmlSelectManyCheckbox) component;
                com.resetValue();
            }

            clearAllComponentInChilds(component.getFacetsAndChildren());    

        }

}