Development Steps: Creating Action Classes
Creating an Error Handling ActionThe If you specify an error handler in your portlet deployment descriptor file ( If your portlet initializer encounters an exception, the error handler will not be called. If you want to ensure that the error handler will execute, you can store the exception object in the portlet context. Then, in your action class's /**Check the PortletContext for an exception object. If * present, throw it so that the error handler will kick in. * @param context the PortletContext */ private static void errorCheck(PortletContext context) throws Exception { Exception e = (Exception) context.getAttribute("PORTLET_EXCEPTION"); if (e != null) { throw e; } } Here is a simple error handler that logs the exception and calls a static error page. The error page supplies a general error message from the portlet's localized resource bundles. import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sas.portal.portlet.PortletContext; import com.sas.services.logging.LoggingServiceInterface; import com.sas.portal.portlet.ErrorHandlerInterface; import com.sas.portal.portlet.NavigationUtil; /**Error handler for some portlets. * It logs the exception and returns ErrorPage.jsp * for the portlet to display. */ public class MyErrorHandler implements ErrorHandlerInterface { private final String _loggingContext = this.getClass().getName(); /**Returns the URL for the portlet controller to call. This is the * name of the error page JSP. * @param request the HttpServletRequest * @param response the HttpServeltResponse * @param context the PortletContext * @param exception the exception thrown by a portlet action * @return the URL to call */ public String service(HttpServletRequest request, HttpServletResponse response, PortletContext context, Exception thrownException) { [code omitted for obtaining a LoggerInterface] //prepare the localized resources for use by the jsp. try { NavigationUtil.prepareLocalizedResources( "com.mycompany.portlets.Resources", request, context); } catch (java.io.IOException ioe) { logger.error(ioe.getMessage(), _loggingContext, ioe); } logger.error(thrownException.getMessage(), _loggingContext, thrownException); return "ErrorPage.jsp"; } } |