Struts2 Exception Handling

Source: Internet
Author: User

Struts2 Exception Handling,

In web development, exceptions are often handled, so that no matter what operations the user performs, the exception page is not displayed to the user, which can give the user a good feeling, now let's talk about struts2's exception handling in development.

1. handle exceptions in servlet. Add the following code to the configuration file in web. xml:
<! -- Handle exceptions -->
<Error-page>
<Error-code> 404 </error-code>
<Location>/commons/error404.jsp </location>
</Error-page>
<Error-page>
<Exception-type> java. lang. NullPointerException </exception-type>
<Location>/commons/errornull. jsp </location>
</Error-page>
2. For exception Handling in struts2, the exception interceptor must be added first, and the default interceptor Stack has already been added to this interceptor, so no special declaration is required. There are two types of struts2 exception ing declarations: global declaration and local declaration.
Note: When an exception occurs, struts2 searches for the ing closest to the exception in the inheritance relationship in the exception ing File Based on the thrown exception. For example, action throws an IOException, which is an Exception.

Directly subclass. So it will match the Exception ing. In addition, action throws a BatchUpdateException, which is both a subclass of SQLException and a subclass of Exception. However, it follows SQLException

The bearing relationship is the closest, so it will match SQLException
<Struts>
<Package name = "default">
...
<Global-results>
<Result name = "login" type = "redirect">/Login. action </result>
<Result name = "Exception">/Exception. jsp </result>
</Global-results>

<Global-exception-mappings>
<Exception-mapping exception = "java. SQL. SQLException" result = "SQLException"/>
<Exception-mapping exception = "java. lang. Exception" result = "Exception"/>
</Global-exception-mappings>
...
<Action name = "DataAccess" class = "com. company. DataAccess">
<Exception-mapping exception = "com. company. SecurityException" result = "login"/>
<Result name = "SQLException" type = "chain"> SQLExceptionAction </result>
<Result>/DataAccess. jsp </result>
</Action>
...
</Package>
</Struts>
Note: Global exceptions can only be configured under the package. They only take effect for the following actions. struts does not have exceptions as in struts. Use together with local and global exceptions.


Three examples show that if an exception occurs during access to DataAccess
One java. SQL. SQLException will be linked to another action SQLExceptionAction
A com. company. SecurityException will be redirected to Login. action.
Any other exceptions that inherit java. lang. Exception will go to Exception. jsp

Exception value in the value Stack: by default, when an Exception occurs, struts2 will press two values into the value Stack
Exception object itself
Predictionstack
Stack trace Value

Exception information can be printed in jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "s" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> system exception </title>
</Head>
<Body>
<H3> error message. Contact the Administrator.
<S: actionerror/>
<P>
<S: property value = "% {exception. message}"/>
</P>
<H3> details
<P>
<S: property value = "% {exceptionStack}"/>
</P>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.