Handle exception in action methods
zjda Feb 2, 2010 6:00 PMHi, I want a general way to handle exceptions in the action/actionListener methods. Here is the action method:
public String refreshObjectAction() throws Exception {
try {
throw new Exception("test error message!");
} catch (Exception e) {
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
response.sendError(Constants.SC_INTERNAL_SERVER_ERROR, e.getMessage());
response.setStatus(Constants.SC_INTERNAL_SERVER_ERROR);
fc.addMessage("errorPanel", new FacesMessage(FacesMessage.SEVERITY_FATAL, e.getMessage(), e.toString()));
fc.responseComplete();
}
return null;
}
I try to use rich:modalPanel to display the error message from the exception:
<a4j:outputPanel ajaxRendered="#{not (facesContext.maximumSeverity==null)}">
<rich:modalPanel id="errorPanel" zindex="2000" resizeable="false" minWidth="100"
minHeight="40" showWhenRendered="true">
<f:facet name="header">Error</f:facet>
<f:facet name="controls">
<h:graphicImage value="/images/close.gif" style="cursor:pointer" onclick="Richfaces.hideModalPanel('errorPanel');" />
</f:facet>
<rich:panel styleClass="main_error_panel">
<h:panelGrid columns="2" width="100%" columnClasses="image_col, message_col"
style="margin:0px; padding:0px; border:5px">
<h:graphicImage value="/images/bigError.gif" />
<rich:messages/>
</h:panelGrid>
<a4j:commandButton value="OK" styleClass="dialog_button"
onclick="Richfaces.hideModalPanel('errorPanel'); return false;">
</a4j:commandButton>
</rich:panel>
</rich:modalPanel>
</a4j:outputPanel>
However, the dialog does not show up after the action method is called. If I add the following code:
A4J.AJAX.onError = function(req, status, message)
Richfaces.showModalPanel('errorPanel');
}
The dialog is displayed, but there is no message shown in the dialog. I want this dialog to display exception errors only, not the validation messages. Any suggestion will be appreciated.
-ZJ