I figured out how to do this. I'm not sure if this is the best or easiest, but it works. I found the Struts Exception Configuration Guide and added the following to the <package> inside struts.xml :
<global-results> <result name="exception">/exception.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="exception" /> </global-exception-mappings>
This will redirect all unhandled exceptions to /exception.jsp . And here is the content of the JSP:
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page contentType="application/json; charset=UTF-8" %> <% response.setStatus(500); %> {"success": false,"errors": "<s:property value="%{exception.message}"/>"}
In the third line, you will notice that I manually set the response code to 500.
This gave me a new problem: exceptions were no longer logged. As suggested in the above Struts manual, I solved this by adding the following to my <package> in struts.xml :
<interceptors> <interceptor-stack name="appDefaultStack"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="appDefaultStack" />
Struts: -1 to create such a simple and obvious NOT function by default, and another -1 to make the solution so dumb.
curtisdf
source share