`
yido5566
  • 浏览: 40731 次
  • 性别: Icon_minigender_1
  • 来自: 福州
最近访客 更多访客>>
社区版块
存档分类
最新评论

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;    
          
    }     

     

    分享到:
    评论
    1 楼 holdbelief 2011-02-09  
    哥哥,你这个到底是Struts几?

    相关推荐

    Global site tag (gtag.js) - Google Analytics