Struts 2 Error Handling. How to get stackTrace attribute and exception?

I work with Struts2 and I have a problem with the error handling mechanism. I would like to get stackTrace and exception attributes to use it inside an action class (for example, to print to the console) or inside a JSP page (without using taglib).

The following is a snippet of my struts.xml file:

<default-interceptor-ref name="defaultStack"/>

    <global-results>
        <result name="Exception" type="redirect">/error.action</result>
    </global-results>

    <global-exception-mappings>
        <exception-mapping result="Exception"   exception="java.lang.Exception" />
    </global-exception-mappings>

    <action name="error" class="fend.ErrorAction">
        <result>/error.jsp</result>
        <interceptor-ref name="configStack"/>
    </action>

Thanks in advance!

+3
source share
3 answers

in your JSP:

Exception : <s:property value="%{exception}" />

Exception stacktrace: <s:property value="exceptionStack"/>
+1
source

I have the same setup as you, except that I redirect my exception to the JSP page exception.jsp.

:

 <s:set name="ex" value="%{exception}" scope="page"/>

, lib, , , ? , . ( , )

JSP, s: set JSP:

<%
Exception exMsg = (Exception)pageContext.getAttribute("ex");
logger.logException(application.getRealPath("")+ "/WEB-INF/error.txt",exMsg);
%>

<br/><br/>User friendly message here

logger Exception :

StackTraceElement[] stackTrace = exception.getStackTrace();

, , , - , ... - ,

0

You can use com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor to log your exceptions at the right log level (and the right logger too). ( Read the documentation here )

The easiest way is to configure defaultStack in your struts.xml, as shown below:

<interceptor-stack name="defaultStack">
  <interceptor-ref name="exception">
     <param name="logEnabled">true</param>
     <param name="logCategory">com.mycompany.app.unhandled</param>
     <param name="logLevel">WARN</param>
  </interceptor-ref>
  <interceptor-ref name="alias"/>
  <interceptor-ref name="servletConfig"/>
  ........
</interceptor-stack>
0
source

Source: https://habr.com/ru/post/1751481/


All Articles