Next I will introduce some notes about exception handling that I encountered when studying Struts2. I hope this method will be helpful to you.
1. struts2 configuration instructions
Struts Exception Handling configuration file name is strtus-default.xml
The file fragment is as follows:
The Code is as follows: |
Copy code |
<Interceptiontor> <! -- Interceptor for exception handling --> <Interceptor name = "exception" class = "com. opensymphony. xwork. interception. ExceptionMapping. Interception"/> <! -- Struts2 default interception Stack --> <Interceptor-stack name = "defaultStack"> <! -- Reference the exception ing interceptor --> <Interceptiontor-ref name = "exception"/> </Interceptor-stack> </Interceptiontor> |
========================================================== ========================
The above is just a struts2 structure. The following are the steps for configuration.
========================================================== ========================
1. Description
The exception handling mechanism of. Struts2 is completed by configuring the <exception-mapping.../> element in the struts. xml file. When configuring the element, you must specify the following two attributes:
Exception: This attribute specifies the exception type set for the exception ing.
Result: When this attribute is specified for an Action, the system returns the logical view name corresponding to the result property value.
According to the location of the <exception-mapping.../> element, the exception ing can be divided into the following two types.
Local exception ing
Global exception ing
Similar to the <result.../> element configuration result, global ing is valid for all actions, but local exception ing is only valid for the Action in which the exception ing is located. If the same exception type is configured for the local exception ing and global exception ing, the local exception ing in this Action overwrites the global exception ing.
2. instance struts2.xml Configuration
The Code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "GBK"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1.7 // EN" Http://strtus.apache.org/dtds/struts-2.1.7.dtd> <Struts> <Package name = "Rudiment" extends = "strtus-default"> <! -- Define global results --> <Global-results> <! -- Two global results are configured below --> <Result name = "SQL">/exception. jsp </result> <Result name = "root">/exception. jsp </result> </Global-results> <! -- Define global exception ing --> <Global-exception-mappings> <! -- When java. SQL. SQLException occurs, struts will jump to SQL --> <Exception-mapping exception = "java. SQL. SQLException" result = "SQL"/> <! -- When java. lang. Exception occurs, struts will jump to the root --> <Exception-mapping exception = "java. lang. Exception" result = "root"/> </Global-exception-mappings> <Action name = "login" class = "org. Rudiment. Action. LoginAction"> <! -- Defines a local exception ing. When a MyException exception is encountered in the Action, strtuts will be transferred to the result named my --> <Exception-mapping exception = "org. Rudiment. Exception. MyException" result = "my"/> <! -- Three partial results are configured below --> <Result name = "my">/exception. jsp </result> <Result name = "error">/error. jsp </result> <Result name = "success">/welcom. jsp </result> </Action> </Package> </Struts> |
When a myException occurs in the action, it is configured as a local exception first.
When java. SQL. SQLException occurs in the action, it will be preferentially handled by the external exception configuration.
Note: The result property value of global exception ing usually does not use local results (in a strange place, local results are preferentially mapped due to global exceptions, if the corresponding local result is not found, the global result can be used.