struts2异常处理

 

  通过Servlet异常处理,只要简单地在Web.xml里配置异常处理类型和返回页面   通过Struts的异常处理机制,在struts.xml配置局部异常和全局异常   通过编写一个Action 异常基类,子类继承而获得异常处理功能

 

 

 

1) Servlet异常处理xml 代码   在web.xml中配置

<error-page>   
   <error-code>404<error-code>   
   <location>XXX.jsp<location>    
<error-page>   
   
<error-page>   
    <exception-type>java.lang.NullPointerException<exception-type>   
    <location>XXX.jsp<location>    
<error-page>

 

 Struts的异常处理机制,在struts.xml配置局部异常和全局异常

<!-- 全局异常映射 --> 
<global-exception-mappings> 
	<exception-mapping result="error" exception="java.lang.Exception">    
	</exception-mapping> 
</global-exception-mappings> 

<!-- 局部异常映射 --> 
<action name="airTicketAction" class="AirTicketAction"> 
	<result name="order">airticket/airticketorder.jsp</result> 
	<exception-mapping result="nullPoint" exception="java.lang.NullPointerException">
	</exception-mapping>   
	<result name="nullPoint">error.jsp</result>   
</action>

 

3)编写一个Action 异常基类,子类继承而获得异常处理功能

 

public abstract class ErrorBaseAction extends Action{       
                 
       public final ActionForward execute(ActionMapping mapping,       
                                    ActionForm form,        
                                    HttpServletRequest request,        
                                    HttpServletResponse response)       
                                    throws IOException, ServletException {        
              try{    
                     return doExecute(mapping,form,request,response);    
              }catch(Exception ex){    
                     return doException(ex,mapping,form,request,response);    
              }    
    }       
      
      public  ActionForward doException(Exception ex,    
                                    ActionMapping mapping,       
                                    ActionForm form,        
                                    HttpServletRequest request,        
                                    HttpServletResponse response)       
                                    throws IOException, ServletException {        
                 // 异常处理    
                  return mapping.findForward("XXX");//返回页面    
      }    
   
       public abstract  ActionForward doExecute(ActionMapping mapping,       
                                    ActionForm form,        
                                    HttpServletRequest request,        
                                    HttpServletResponse response)       
                                    throws IOException, ServletException;    
      
}     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值