When a request is sent in a JSP page, an error may occur when the request is processed by the system. If the exception (error) is not handled during the request, then the exception information page is returned to the user, and the error can be handled in order to avoid directly presenting the error page to the user.
1. Layered anomalies
background processing in the system is generally divided into 3 layers , respectively is Action , Service and the DAO layer , each level is likely to have a corresponding error.
(1) Action layer may appear parsing request parameters, return the result of a problem;
(2) Service layer may appear in the request to do business operations problems, there are problems to determine according to the actual situation will affect the results of this operation, Action in order to determine the abnormal information, and then decide whether the operation was successful;
(3) DAO The layer may also appear to be an error while manipulating the database, and this type of error is generally fatal and can affect the result of the operation.
therefore, in 3 at least two types of exception information must be identified in a hierarchy.
Total Exception class : Sysexception.java
package com.rk.core.exception;//Note that this is the abstract class Public abstract class SysException extends Exception {private String errorMsg;public Sysexception () {super ();} Public sysexception (string message, throwable cause,boolean enablesuppression, Boolean writablestacktrace) {super (message, cause, enablesuppression, Writablestacktrace); errormsg = message;} Public sysexception (String message, throwable cause) {super (message, cause); Errormsg = message;} Public sysexception (string message) {super (message); errormsg = message;} Public sysexception (throwable cause) {super (cause);} {{public string geterrormsg () {return errormsg;} Public void seterrormsg (string errormsg) {this.errormsg = errormsg;} }}}
Action layer Exception class : Acitonexception.java
Package Com.rk.core.exception;public class Actionexception extends Sysexception {public actionexception () {Super (" An action request has an error! ");} Public actionexception (String message) {super (message);}}
Service Layer Exception class: Serviceexception.java
Package Com.rk.core.exception;public class Serviceexception extends Sysexception {public serviceexception () {Super (" Service Business Operation Error! ");} Public serviceexception (String message) {super (message);}}
2. Exception handling
2.1. Global exception Mapping
in the Struts General configuration file ( Struts.xml ), a global exception map and a global result are configured in the Package And then let all the other business modules Struts configuration file ( Struts-*.xml ) to handle the specific exception thrown by the background.
The key parts of global results and exception mappings in Struts.xml
<!-- Configure global results and exception mappings --> <package name= "Base-default" extends= "Struts-default" abstract= "true" > <!-- return result type --> <!-- Global return results --> <global-results> <result name= "Syserror" >/WEB-INF/jsp/error.jsp</result> <result name= "Input" >/WEB-INF/jsp/error.jsp</result> </global-results> <!-- Global Exception map -- > <global-exception-mappings> <exception-mapping result= "Input" exception= "Java.lang.Exception" >< /exception-mapping> </global-exception-mappings> </package>
Complete Struts.xml file
<?xml version= "1.0" encoding= "UTF-8" ?><! Doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en "Http://struts.apache.org/dtds/struts-2.3.dtd" ><struts><!-- Disable dynamic method access -- > <constant name= "Struts.enable.DynamicMethodInvocation" value= "false" /> <!-- Configuration into development mode --> <constant Name= "Struts.devmode" value= "true" /><!-- configuration extension named Action --><constant name= "Struts.action.extention" value= "action" /><!-- Configure the theme to Simple --><constant name= "Struts.ui.theme" value= "simple" /><!-- set up factory to create objects --><constant name= "Struts.objectfactory" value= "Spring" /> <!-- Configuring global results and Exception mapping --> <package name= "Base-default" extends=" Struts-default " abstract=" true "> <!- - return result type --> <!-- Global return result --> <global-results> <result name= "Syserror" >/WEB-INF/jsp/error.jsp</result> <result name= "Input" >/WEB-INF/jsp/error.jsp</result> </global-results> <!-- Global Exception Mapping --> <global-exception-mappings> <exception-mapping result= "Input" exception= " Java.lang.Exception "></exception-mapping> </ Global-exception-mappings> </package> <!-- Struts configuration file with test --><include file= "Com/rk/test/config/struts-test.xml"/><!-- include user-managed struts profiles --><include file= "Com/rk/tax/config/struts-user.xml"/> </struts>
in the sub-business module Sturts-*.xml In the total configuration file that inherits from the Package :
Struts-user.xml (Note: extends= "Base-default")
<?xml version= "1.0" encoding= "UTF-8" ?><! Doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en "Http://struts.apache.org/dtds/struts-2.3.dtd" ><struts> <package name= "User_package" namespace= "/tax" extends= "Base-default" > <action name= "user_*" class= "useraction" method= "{1}" > <result name= "{1}" >/WEB-INF /jsp/tax/user/{1}.jsp</result> <result name= "List" type= "Redirectaction" > <param name= "ActionName" >user_listUI</param> <param name= "namesPace ">/tax</param> </result > </action> </package> </struts>
in the specific A system exception is thrown in the background processing code:
Useraction.java
List page public String Listui () throws actionexception{try {int i = 1/0;userlist = Userservice.findall ();} catch (Exception e) {throw new actionexception ("Request operation failed:" + e.getmessage ());} return "Listui";}
if the Action only thrown in the actionexception , if you configure the global exception map when the exception is not present, java.lang.Exception the mapping of exception classes can also be captured.
SSH series: (14) System exception handling