I have a JSF page that contains two subviews. Depending on which commandButton is executed, one of the subviews is rendered. However, if validation fails on one of the subviews, the error messages stay visible and the components are still invalid the next time the subview is rendered. I have a workaround in my bean to remove the messages but the components stay invalid. Is there a way to set the components as valid or create a new view for the subview only?
Here is my work around for the messages:
Iterator messageIterator = FacesContext.getCurrentInstance().getMessages();
while (messageIterator.hasNext())
{
messageIterator.remove();
}
This is caused by the fact that when
renderedis false the component is not shown, but it still is in the component tree for the view. So the component does not show but its values and state are still there.There are several ways around this:
Use JSTL tags such as
<c:if />(if at all possible) instead ofrendered. Page elements inside a<c:if/>tag will be in the tree only if the condition is true.Don’t do a postback when passing from one subview to the other. This effectively recreates the tree from scratch. For example perform a GET instead of a POST.
Invalidate the view so that the tree is recreated. Make the
commandButtonthat switches subviewsimmediate="true"and invalidate the view adding this code:sub1(this requiresimmediate="true"as well):this link provides other minor ways of achieving this.