struts2.1.6 Exception 08

ExceptionHandling


1. 在Actoin中進行異常映射


2. 在package中進行全局異常映射

3. 使用繼承共用異常映射

4. Struts2中異常處理由攔截器實現(觀察struts-default.xml)

a) 實際上Struts2的大多數功能都由攔截器實現
  1. <exception-mapping result="error" exception="java.sql.SQLException"/>  
  2.             <result name="error">/error.jsp</result>  


  1. public List<Category> list()throws SQLException {  
  2.         Connection conn = DB.createConnection();  
  3.         String sql = "select * from _category_";  //測試聲明事異常 表名錯  
  4.         PreparedStatement pstm = DB.prepare(conn, sql);  
  5.         List<Category> list = new ArrayList<Category>();  
  6.         try {  
  7.             ResultSet rs = pstm.executeQuery();  
  8.             Category c = null;  
  9.             while(rs.next()) {  
  10.                 c = new Category();  
  11.                 c.setId(rs.getInt("id"));  
  12.                 c.setName(rs.getString("name"));  
  13.                 c.setDescription(rs.getString("description"));  
  14.                 list.add(c);  
  15.             }  
  16.         } catch (SQLException e) {  
  17.             e.printStackTrace();  
  18.             throw(e);  
  19.         }  
  20.         DB.close(pstm);  
  21.         DB.close(conn);  
  22.         return list;  
  23.     }  
  24.   
  25. public String list() throws Exception {  
  26.         categories = categoryService.list();  
  27.         return SUCCESS;   
  28.     }  
  29.   
  30. <package name="front" namespace="/" extends="struts-default" >  
  31.        <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">  
  32.                <result>/admin/Category_list.jsp</result>  
  33.                <exception-mapping result="error" exception="java.sql.SQLException"/>  
  34.                <result name="error">/error.jsp</result>  
  35.        </action>  
  36.     </package>  

  1. <global-exception-mappings>  
  2.             <exception-mapping result="exception_handle" exception="Exception"></exception-mapping>  
  3.         </global-exception-mappings>  
映射所有異常


  1. 常用方式  
  2. <constant name="struts.devMode" value="true"/>  
  3.     <package name="bbs2011_default" extends="struts-default">  
  4.         <global-results>  
  5.             <result name="error">/error.jsp</result>  
  6.         </global-results>  
  7.          <global-exception-mappings>  
  8.             <exception-mapping result="error" exception="java.lang.Exception"/>  
  9.         </global-exception-mappings>  
  10.     </package>  
  11.          


  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <constant name="struts.devMode" value="true"/>  
  8.     <package name="bbs2011_default" extends="struts-default">  
  9.         <global-results>  
  10.             <result name="error">/error.jsp</result>  
  11.         </global-results>  
  12.          <global-exception-mappings>  
  13.             <exception-mapping result="error" exception="java.lang.Exception"/>  
  14.         </global-exception-mappings>  
  15.     </package>  
  16.          
  17.          
  18.     <package name="admin" namespace="/admin" extends="struts-default" >  
  19.           
  20.   
  21.         <action name="index">  
  22.             <result>/admin/index.html</result>  
  23.         </action>  
  24.           
  25.       
  26.        <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">  
  27.             <result>/admin/{1}_{2}.jsp</result>  
  28.             <result name="input">/admin/{1}_{2}.jsp</result>  
  29.               
  30.        </action>  
  31.        <!--   
  32.        <action name="category" class="com.gz.bbs2011.action.{1}Action">  
  33.         <result name="add_input">/admin/Category_add_input.jsp</result>  
  34.         <result name="update_input">/admin/Category_update_input.jsp</result>  
  35.        </action>  
  36.         -->  
  37.     </package>  
  38.        
  39.     <package name="front" namespace="/" extends="bbs2011_default" >  
  40.        <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">  
  41.             <result>/admin/Category_list.jsp</result>  
  42.    <!--          <exception-mapping result="error" exception="java.sql.SQLException"/>  
  43.             <result name="error">/error.jsp</result> -->  
  44.        </action>  
  45.     </package>  
  46.       
  47.   
  48. </struts>  

  1.         <default-action-ref name="Category_list"/>  
  2.        <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">  
  3.             <result>/admin/{1}_{2}.jsp</result>  
  4.             <result name="input">/admin/{1}_{2}.jsp</result>      
  5.        </action>  
  6.   
  7.   
  8. <default-action-ref name="Category_list"/>  如果裏面action裏面有class不可執行 不能執行  
  9.   
  10. action  (慎用此方法,儘量用其他方式解決)  
  11.   
  12.   
  13.   
  14. 解決默認執行方法如下  
  15. web.xml  
  16. <welcome-file>index</welcome-file>  
  17. struts.xml  
  18.   <package name="front" namespace="/" extends="bbs2011_default" >  
  19.           
  20.   
  21.         <default-action-ref name="Category_list"/>   <!-- bug -->  
  22.   
  23.        <action name="index" class="com.gz.bbs2011.action.CategoryAction" method="list">       
  24.   
  25.         
  26.     <result>/admin/Category_list.jsp</result>      
  27.   </action>      
  28. </package>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章