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