bdeo
bdeo

Reputation: 113

Invalidate session followed by redirect recreates the bean

Working on a JSF validator method defined in a @SessionScoped bean:

public void doValidation(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    // throws ValidatorException on invalid input
    ...
    // But, in some cases we want to lock out the user by invalidating session and redirecting to a different error page
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.invalidateSession();
    ec.redirect(errorPage);
}

However, the behavior I am seeing on the redirect is, instead of/ before going to the error page, the @SessionScoped bean seems to be getting recreated (its constructor and @PostConstruct is being called). When the invalidateSession() call is removed, it directly goes to the errorPage without recreating the bean. Note that, the error page has no references to this bean.

Upvotes: 0

Views: 231

Answers (0)

Related Questions