Given the following tag handler class.
public final class ViewParamValidationFailed extends TagHandler implements ComponentSystemEventListener
{
private
By default, JSF serializes the view state (the component tree state) for any <h:form>
in the view (as <input type="hidden" name="javax.faces.ViewState">
). This is restored during the restore view phase of the postback request on that form. The JSF view state covers among others the component system event listeners of the components in the tree.
This line
((UIViewRoot) parent).subscribeToEvent(PostValidateEvent.class, this);
adds one to the UIViewRoot
component and this then needs to be serialized.
You've 4 options:
SystemEventListener
instead of ComponentSystemEventListener
.In this particular case, as long as you don't need to support the nesting of the tag inside the individual <f:viewParam>
, but only in <f:metadata>
, then option 4 should suffice. Replace the ComponentSystemEventListener
interface by SystemEventListener
, implement the isListenerForSource()
method to return true for UIViewRoot
only, finally use UIViewRoot#subscribeToViewEvent()
instead to subscribe the listener.
public final class ViewParamValidationFailed extends TagHandler implements SystemEventListener {
private final String redirect;
public ViewParamValidationFailed(TagConfig config) {
super(config);
redirect = getRequiredAttribute("redirect").getValue();
}
@Override
public void apply(FaceletContext context, UIComponent parent) throws IOException {
if (parent instanceof UIViewRoot && !context.getFacesContext().isPostback()) {
((UIViewRoot) parent).subscribeToViewEvent(PostValidateEvent.class, this);
}
}
@Override
public boolean isListenerForSource(Object source) {
return source instanceof UIViewRoot;
}
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.isValidationFailed()) {
try {
ExternalContext externalContext = context.getExternalContext();
externalContext.redirect(externalContext.getRequestContextPath() + redirect);
}
catch (IOException e) {
throw new AbortProcessingException(e);
}
}
}
}