Java EE SSH Framework Integration (iii) STRUTS2 exception, HTTP error status code processing

Source: Internet
Author: User
Tags error status code

Struts2 action may not be accessible, or the action report exception, etc., so need to do some processing, give the user a friendly impression.


1. Exception handling result declaration in action

<action name= "book_*" class= "com.stone.action.BookAction" method= "{1}" ><result name= "{1}" type= "Dispatcher ">/web-inf/jsp/book_{1}.jsp</result><result name=" Error-result ">/web-inf/jsp/error_result.jsp </result><exception-mapping result= "Error-result" exception= "Java.lang.Exception"/></action>
In action, a result named "Error-result" is defined, which is now mapped to "Error-result" when Java.lang.Exception is captured in the action, which jumps to Error_ result.jsp

Note: Exception can be any one runtimeexception and can be a custom exception.


2. Exception handling result using global result

<global-results>    <result name= "Exceptionerror" type= "Dispatcher" >/web-inf/jsp/error/struts_ Exception.jsp</result></global-results><global-exception-mappings>    <exception-mapping result= "Exceptionerror" exception= "Java.lang.Exception"/></global-exception-mappings>

Defines a global result, named Exceptionerror. Global scope, when capturing to java.lang.Exception, maps to Exceptionerror and jumps to the corresponding JSP.

3. Use the default action when the action for the access address does not exist

<default-action-ref name= "error"/><action name= "error" >    <result>/web-inf/struts_ Erroraction.jsp</result></action>

Defines the default action-reference, which is referenced from the following action-error. The default action is called when the action that resolves to the access does not exist.


4. Use the Web. XML configuration When accessing pages, resources, etc. that do not exist

<error-page><error-code>404</error-code><location>/WEB-INF/404.html</location> </error-page>
Error-code Error HTTP status code, location map to address

5.web.xml can also handle exception, which handles exceptions on servlets and dynamic pages.

<error-page><!--will trigger--><exception-type>java.lang.nullpointerexception when hearing related exceptions reported in servlet and dynamic Web pages </exception-type><location>/WEB-INF/exception.html</location></error-page>





Java EE SSH Framework Integration (iii) STRUTS2 exception, HTTP error status code processing

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.