This is a common issue, but he could not find a quick solution so I wrote one.
This function fetches the parent component which in his case was a form containing
<h:inputText/>
components, and then clears them. You may need to add some more code to do it recursively if the components are nested.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public String clear( final String parentComponentId) { UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot(); UIComponent fc = view.findComponent(parentComponentId); if ( null != fc) { List<uicomponent> components = fc.getChildren(); for (UIComponent component : components) { if (component instanceof UIInput) { UIInput input = (UIInput) component; // JSF 1.1+ // input.setSubmittedValue(null); // input.setValue(null); // input.setLocalValueSet(false); // input.setValid(true); // JSF 1.2+ input.resetValue(); } } } return null ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < h:head > < title >Reset and Clear Example</ title > </ h:head > < h:body > < h:form id = "form1" > < h:panelGrid id = "panelGrid1" columns = "2" > < h:inputText id = "name" value = "#{indexBean.name}" /> < h:message for = "name" /> < h:panelGroup > < h:commandButton value = "Submit" /> < h:commandButton type = "reset" action = "#{indexBean.clear('form1')}" value = "Clear and Reset" > < f:ajax execute = "@this" render = "panelGrid1" /> </ h:commandButton > </ h:panelGroup > </ h:panelGrid > </ h:form > </ h:body > </ html > |
0 comments :
Post a Comment