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.
|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
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-/Apache Software Foundation//DTD Struts
- Configuration 2.1//EN" "http://struts.apache.org/dtds/struts 2.1.dtd">
- <package name="aa" extends="struts-default">
- <result name="myresult">globalresult.jsp</result>
- <global-exemption mappings>
- <exception-mapping result="myresult" exception="java.lang.Exception"></exception-mapping>
- </global-special case mappings>
- <action name="login" class="com.Login">
- <result name="error">error.jsp</result>
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.
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
Full case of special case handling
The other expected assets to finish this precedent are as follows:
- Input page (index.jsp)
- Action class (Login.java)
- 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
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
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