Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to fix a java.lang.IllegalStateException: Cannot forward a response that is already committed on a jsf action with sendRedirect/forward in ADF
//ADF : jsf :
<h:commandButton id="myBtnId"
action="#{myBean.doLogic}"
value="click me"
type="submit"
actionListener="#{myBean.forwardAfterLogic}"/>
//ADF : myBean :
public String doLogic() {
//Add business logic that will be executed first when the button is clicked
}
public void forwardAfterLogic(ActionEvent actionEvent) {
// Add forward logic here ...
//This will be execute after doLogic() in the second part of the lifecycle
//example:
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
String url = ectx.getRequestContextPath() + "/pageToRedirectTo";
try {
response.sendRedirect(url);
FacesContext.getCurrentInstance().responseComplete();
} catch (IOException e) {
//Do something and add trace
} catch (IllegalStateException il) {
//Do something and add trace
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment