Struts2 Exception Handling Mechanism

Source: Internet
Author: User

Struts2 uses declarative methods to manage Exception Handling, so we do not need to write a lot of try... catch... statement to capture exceptions. The execute method throws all exceptions and submits them to the struts2 framework for processing. the XML file is configured with an abnormal ing mechanism, so that struts2 can process and transfer the corresponding view resources.


Exception ing can be divided into global exception ing and local exception ing. It has the same nature as <result.../>.


The configuration code is as follows:




XML Code

<Struts>

<Package name = "struts2" extends = "struts-Default">

<! -- Configure global exception ing -->

<Global-exception-mappings>

<Exception-mapping result = "SQL" exception = "Java. SQL. sqlexception"/>

</Global-exception-mappings>

<Action name = "login" class = "com. Test. Action. loginaction">

<! -- Configure local exception ing -->

<Exception-mapping result = "myexception" exception = "com. Test. Exception. myexception"/>

<Result name = "success"> result. jsp </result>

<Result name = "SQL"> error. jsp </result>

<Result name = "myexception"> error. jsp </result>

</Action>

</Package>

</Struts>


<Struts>

<Package name = "struts2" extends = "struts-Default">

<! -- Configure global exception ing -->

<Global-exception-mappings>

<Exception-mapping result = "SQL" exception = "Java. SQL. sqlexception"/>

</Global-exception-mappings>

<Action name = "login" class = "com. Test. Action. loginaction">

<! -- Configure local exception ing -->

<Exception-mapping result = "myexception" exception = "com. Test. Exception. myexception"/>

<Result name = "success"> result. jsp </result>

<Result name = "SQL"> error. jsp </result>

<Result name = "myexception"> error. jsp </result>

</Action>

</Package>

</Struts>


To display the exception information on the exception handling page, you can use the <s: property/> label to output the exception information:




<S: property value = "exception"/>: Output exception information


<S: property value = "exceptionstack"/>: outputs exception stack information.
Related Article

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.