Struts 2 Exception Handling - exception interceptor


In our web application, there might happen special case anytime.

To defeat this issue, struts 2 gives a system of global exemption handling where we can show a worldwide outcome to the user.

Struts 2 consequently log the uncaught special cases and diverts the client to the mistake handler page.


Understanding the inner working of special case interceptor

If there happens special case, it is enveloped by ExceptionHolder and pushed in the valuestack so we can without much of a stretch access exemption object from the result.

Parameters of special case interceptor

There are 3 parameters characterized for special case interceptor. All are optional.

ParameterDescription
logEnabled determines log ought to be empowered or not. You can pass genuine or false.
logLevel determines the log level. It might be follow, troubleshoot, information, caution, mistake, deadly. Default log level is debug.
logCategory determines the log class eg. com.mycompany.app. The default is com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor..

Example of special case taking care of in struts 2

For exemption taking care of, we determine the worldwide outcome and worldwide special case mappings in the struts.xml file.

struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-/Apache Software Foundation//DTD Struts  
  3.  Configuration 2.1//EN" "http://struts.apache.org/dtds/struts 2.1.dtd">  
  4. <struts>  
  5. <package name="aa" extends="struts-default">  
  6.   
  7. <global-results>  
  8. <result name="myresult">globalresult.jsp</result>  
  9. </global-results>  
  10.   
  11. <global-exemption mappings>  
  12. <exception-mapping result="myresult" exception="java.lang.Exception"></exception-mapping>  
  13. </global-special case mappings>  
  14.   
  15. <action name="login" class="com.Login">  
  16. <result>welcome.jsp</result>  
  17. <result name="error">error.jsp</result>  
  18. </action>  
  19.   
  20. </package>  
  21. </struts>      

The global-results sub-component of bundle determines the worldwide outcome for this package.

The result sub-component of worldwide outcome determines the outcome page that will be rendered to the client as a view.

The global-exemption mappings sub-component of bundle indicates the special case mapping for every one of the activities of this package.

The exception-mapping sub-component of worldwide special case mapping maps the given outcome for the given exemption type. In this precedent, we are utilizing the Exception which the parent of numerous exemption classes, for example, IOException, ArithmeticException and so on. It implies if any exemption happens, determined outcome will be invoked.

Displaying exception

We can show the special case on the program by printing the exemption or exceptionStack.

The exemption object prints the special case name while exceptionStack prints the special case details.

globalresult.jsp
  1. <p>Exception Name: <s:property value="exception" /> </p>  
  2. <p>Exception Details: <s:property value="exceptionStack" /></p>  

Full case of special case handling

The other expected assets to finish this precedent are as follows:

  1. Input page (index.jsp)
  2. Action class (Login.java)
  3. View parts (globalresult.jsp, welcome.jsp and error.jsp)

1) Create index.jsp for information

This jsp page makes a structure utilizing struts UI labels. It gets name and secret word from the client.

index.jsp
  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2.   
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <s:password name="password" label="Password"></s:password>  
  6. <s:submit value="login"></s:submit>  
  7. </s:form>  

2) Create the activity class

This activity class contains two fields name and secret key and one technique execute. Here, we are tossing exemption self if secret phrase matches struts.

This is sham precedent, on the off chance that you remark the referenced line in the execute technique, exemption won't occur.

Login.java
  1. package com;  
  2. public class Login {  
  3. private String name,password;  
  4. //getters and setters  
  5.   
  6. public String execute(){  
  7.     if(password.equals("struts")){  
  8.         int a=12/0;//If you comment this, exception will not occur  
  9.         return "success";  
  10.     }else  
  11.         return "error";  
  12. }  
  13. }  

3) Create see components

There are three view parts globalresult.jsp that shows the exemption message, welcome.jsp that shows the appreciated message with the username and error.jsp that shows the mistake message.

globalresult.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Sorry an exception occured!  
  4. <p>Exception Name: <s:property value="exception" /> </p>  
  5. <p>Exception Details: <s:property value="exceptionStack" /></p>  
  6. <form>  
  7. <input type="button" value="back" onclick="history.back()">  
  8. </form>  
welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. Welcome, <s:property value="name"/>  
error.jsp
  1. Sorry username or password error!  
  2. <jsp:include page="index.jsp"></jsp:include>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition