struts2.1.6 Exception 08

ExceptionHandling


1. 在Actoin中进行异常映射


2. 在package中进行全局异常映射

3. 使用继承共用异常映射

4. Struts2中异常处理由拦截器实现(观察struts-default.xml)

a) 实际上Struts2的大多数功能都由拦截器实现

<exception-mapping result="error" exception="java.sql.SQLException"/>
       		<result name="error">/error.jsp</result>


public List<Category> list()throws SQLException {
		Connection conn = DB.createConnection();
		String sql = "select * from _category_";  //测试声明事异常 表名错
		PreparedStatement pstm = DB.prepare(conn, sql);
		List<Category> list = new ArrayList<Category>();
		try {
			ResultSet rs = pstm.executeQuery();
			Category c = null;
			while(rs.next()) {
				c = new Category();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw(e);
		}
		DB.close(pstm);
		DB.close(conn);
		return list;
	}

public String list() throws Exception {
        categories = categoryService.list();
        return SUCCESS; 
    }

<package name="front" namespace="/" extends="struts-default" >
       <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">
               <result>/admin/Category_list.jsp</result>
               <exception-mapping result="error" exception="java.sql.SQLException"/>
               <result name="error">/error.jsp</result>
       </action>
    </package>



<global-exception-mappings>
    		<exception-mapping result="exception_handle" exception="Exception"></exception-mapping>
    	</global-exception-mappings>
映射所有异常


常用方式
<constant name="struts.devMode" value="true"/>
	<package name="bbs2011_default" extends="struts-default">
		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>
		 <global-exception-mappings>
    		<exception-mapping result="error" exception="java.lang.Exception"/>
    	</global-exception-mappings>
	</package>
	   


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.devMode" value="true"/>
	<package name="bbs2011_default" extends="struts-default">
		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>
		 <global-exception-mappings>
    		<exception-mapping result="error" exception="java.lang.Exception"/>
    	</global-exception-mappings>
	</package>
	   
	   
    <package name="admin" namespace="/admin" extends="struts-default" >
    	

    	<action name="index">
    		<result>/admin/index.html</result>
    	</action>
    	
    
       <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">
       		<result>/admin/{1}_{2}.jsp</result>
       		<result name="input">/admin/{1}_{2}.jsp</result>
       		
       </action>
       <!-- 
       <action name="category" class="com.gz.bbs2011.action.{1}Action">
       	<result name="add_input">/admin/Category_add_input.jsp</result>
       	<result name="update_input">/admin/Category_update_input.jsp</result>
       </action>
        -->
    </package>
     
    <package name="front" namespace="/" extends="bbs2011_default" >
       <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">
       		<result>/admin/Category_list.jsp</result>
   <!--     		<exception-mapping result="error" exception="java.sql.SQLException"/>
       		<result name="error">/error.jsp</result> -->
       </action>
    </package>
    

</struts>


 
 
 
 
    	<default-action-ref name="Category_list"/>
       <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">
       		<result>/admin/{1}_{2}.jsp</result>
       		<result name="input">/admin/{1}_{2}.jsp</result>	
       </action>


<default-action-ref name="Category_list"/>  如果里面action里面有class不可执行 不能执行

action  (慎用此方法,尽量用其他方式解决)



解决默认执行方法如下
web.xml
<welcome-file>index</welcome-file>
struts.xml
  <package name="front" namespace="/" extends="bbs2011_default" >
        

        <default-action-ref name="Category_list"/>   <!-- bug -->

       <action name="index" class="com.gz.bbs2011.action.CategoryAction" method="list">     

      
    <result>/admin/Category_list.jsp</result>    
  </action>    
</package> 



你可能感兴趣的:(exception,struts,list,Class,action,bbs)