Thursday, February 11, 2016

Clear Input Components on Page Refresh - Immediate Setting Issue

It becomes challenging if you want to clear an input form as a result of page refresh, which is  caused by a command component with immediate set to 'true'. For example if you have a page in a task flow which has  an input form and there is a button on that page which again invokes the same task flow but with different parameters. So, as a result of button click you expect  the same input form to appear but do not expect any values to stay on the form fields which were entered previously, basically want it to refresh. As described in Apache Myfaces docs, if the button has immediate set to true, the component tree of the page is not refreshed. One of the ways to clear off previously entered values is to create a new View tree with the following code in the command method action :

public void refresh() {
  FacesContext context = FacesContext.getCurrentInstance();
  Application application = context.getApplication();
  ViewHandler viewHandler = application.getViewHandler();
  UIViewRoot viewRoot = viewHandler.createView(context, context
   .getViewRoot().getViewId());
  context.setViewRoot(viewRoot);
  context.renderResponse(); //Optional
}

Please refer to Apache Myfaces docs for all other ways to achieve the same .

No comments:

Post a Comment