You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2006/01/04 00:01:20 UTC

[Myfaces Wiki] Update of "ClearInputComponents" by SimonKitching

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by SimonKitching:
http://wiki.apache.org/myfaces/ClearInputComponents

New page:
Sometimes you want to provide a command component (eg a link or button) that performs some server action, and
renders the same page but with completely "fresh" values for all components. However this doesn't work well
when the component is "immediate"; in this case input components get their "submitted value" set during the postback
and re-render that submitted value even when the backing values for those input components have been reset to default values.

Here are a number of possible solutions

=== Force a new View ===
Call this method from the action method of the immediate command component:
{{{
  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();
  }
}}}
This causes the current View tree to be discarded and a fresh one created. The new components of course then have no submitted values,
and so fetch their displayed values via their value-bindings.

=== Explicitly clear submitted value ===

For each component that you want to reset, specify a binding attribute so that the backing bean can access them.
>>From the immediate component's action method, call setSubmittedValue(null). This will cause that component to refetch its value
via its value-binding when rendered.

This only affects specific components, though. If you want to clear a whole form, you can bind some parent component, then
explicitly walk the tree of child components testing each one for type !EditableValueHolder, and if so performing the
above action. You could even do this by fetching the UI!ViewRoot component, and not need to use any component bindings
at all.

=== Return a non-null navigation value ===

Commonly, null is returned from the action method of the immediate command component. This causes the current view to be re-rendered.
If this method returns a string that maps to the same view, then this may cause a ''new component tree'' for that page to be created
and rendered, rather than re-rendering the current component tree.

Note that this approach has not been tested; if you find it does work, then please update this page.
It's not also currently clear whether this behaviour is required by the spec, or is implementation-specific. If you find something
in the spec about this, please also update this page with that information.