现在的位置: 首页 > 综合 > 正文

struts2 对声明式异常的处理 –> 原理:通过拦截器(Interceptor)

2012年02月14日 ⁄ 综合 ⁄ 共 5666字 ⁄ 字号 评论关闭
  ++++++++++++++++++++++++++++++++++++++++++++++++++++
    struts2 声明式异常的处理 --> 原理:通过拦截器(Interceptor):
        三步曲(A、B、C)
  ++++++++++++++++++++++++++++++++++++++++++++++++++++

  
简介

<s>++++++++++++++++++++++++++++++++++++++++++++++++++++++++
声明式异常处理
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

struts.xml中

1.在<action>中进行{局部异常映射} --> 针对 某个 action

2.在<package>中进行{全局异常映射}

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

<package name="bbs_default" extends="struts-default"> //struts-default.xml中 定义了许多 interceptor
<!-- 全局异常映射 //异常处理的 一般做法
1.<global-exception-mappings>
2.<global-results>
3.<global-results> 在 <global-exception-mappings> 之前
-->

<global-results>
<result name="sqlException">/sqlException.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping result="sqlException" exception="java.sql.SQLException"/>
</global-exception-mappings>

</package>

<package name="admin" namespace="/admin" extends="bbs_default">...</package>//admin --> bbs_default

4.Struts2中异常处理由 interceptor 实现 (<观察 struts-default.xml>)

@struts2 中<大多数功能 都由 interceptor 实现>:
#<对参数的处理>
#<将HttpRequest 转换成 Map类型的 Request>
   
CategoryService.java    //《《A》》

 1 public class CategoryService {
2
3 //throws SQLException 抛出异常 --> 在struts.xml设置,对Exception(各种异常)统一处理
4 public List<Category> list() throws SQLException, NullPointerException{
5
6 List<Category> categories = new ArrayList<Category>();
7
8 Connection conn = DB.createConn();
9 //String sql = "select * from t_category_"; //测试struts对Exception处理
10 String sql = "select * from t_category——";
11
12 PreparedStatement ps = DB.prepare(conn, sql);
13 try {
14 ResultSet rs = ps.executeQuery();
15 while(rs.next()){
16 Category c = new Category();
17 //Category c = null; //测试NullPointerException处理
18 c.setId(rs.getInt(1));
19 c.setName(rs.getString("name"));
20 c.setDescription(rs.getString(3));
21
22 categories.add(c);
23 }
24 DB.close(rs);
25
26 } catch (SQLException e) {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 throw e;
30 }
31
32 DB.close(ps);
33 DB.close(conn);
34
35 return categories;
36 }
37
38 }

CategoryAction.java

 1 public class CategoryAction extends ActionSupport{
2
3 /**
4 * Category category 是 Model,Struts收到传递的 category.name -> 自动调用 new Category(); 并 赋值
5 * List<Category> categories 在 list()中初始化,故无须 new !
6 * CategoryService categoryService 需要 new !
7 *
8 * 注:为所有private 变量 提供 getter、setter
9 */
10 private Category category;
11 private List<Category> categories;
12 private CategoryService categoryService = new CategoryService();
13 private int categoryId;
14
15 //Struts支持对 <异常>的 统一处理 --> struts.xml
16 public String list() throws Exception{ //《《B》》
17 categories = categoryService.list();
18 return SUCCESS;
19 }
20
21 public String add(){
22 categoryService.add(category);
23 return SUCCESS;
24 }
25
26 public String delete(){
27 categoryService.deleteById(categoryId);
28 return SUCCESS;
29 }
30
31 public String update(){
32 categoryService.update(category);
33 return SUCCESS;
34 }
35
36 public CategoryService getCategoryService() {
37 return categoryService;
38 }
39
40 public void setCategoryService(CategoryService categoryService) {
41 this.categoryService = categoryService;
42 }
43
44 public Category getCategory() {
45 return category;
46 }
47
48 public void setCategory(Category category) {
49 this.category = category;
50 }
51
52 public int getCategoryId() {
53 return categoryId;
54 }
55
56 public void setCategoryId(int categoryId) {
57 this.categoryId = categoryId;
58 }
59
60 public String addInput(){
61 return INPUT;
62 }
63
64 public String updateInput(){
65
66 /**
67 * 《Struts2为每次请求都实例化一个Action,因此 Action是 线程安全的》
68 * 1.当点击 --> <a href="admin/Category_list">,struts 实例化一个 CategoryAction,并执行list(),
69 * 返回List<Category> categories --> Category_list.jsp, 并由<s:iterator value="categories">调用,
70 *
71 * 2.当点击 --> <a href="admin/Category_updateInput?categoryId=<s:property value="#c.id"/>">时,
72 * struts 重新实例化一个 CategoryAction, 并执行 updateInput(),所以此时 categories = null
73 */
74 //System.out.println(categories.size()); //categories为空
75 //Category c = categories.get(categoryId); //所以,不能这样
76 //categoryService.update(c);
77
78 category = categoryService.loadCategoryById(categoryId);
79 return INPUT;
80 }
81
82 public void setCategories(List<Category> categories) {
83 this.categories = categories;
84 }
85
86 public List<Category> getCategories() {
87 return categories;
88 }
89
90 }




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
8 <constant name="struts.devMode" value="true" />
9
10 <!-- bbs_default <- front & admin 继承 -->
11 <package name="bbs_default" extends="struts-default">
12
13 <!-- 全局异常映射 //异常处理的 一般做法                         《《第C步》》
14 1.<global-exception-mappings>
15 2.<global-results>
16 3.<global-results> 在 <global-exception-mappings> 之前
17 -->
18
19 <global-results>
20 <result name="sqlException">/sqlException.jsp</result>
21 </global-results>
22
23 <global-exception-mappings>
24 <exception-mapping result="sqlException" exception="java.sql.SQLException"/>
25 </global-exception-mappings>
26
27 </package>
28
29 <!-- front 前台页面 -->
30 <package name="front" namespace="/" extends="bbs_default">
31
32 <default-action-ref name="Category_list"></default-action-ref>
33 <action name="Category_list" class="cn.edu.nuc.action.CategoryAction" method="list">
34 <result>/index.jsp</result>
35 </action>
36
37 </package>
38
39 <!-- admin 后台页面 -->
40 <package name="admin" namespace="/admin" extends="bbs_default">
41
42 <!-- <default-action-ref name="index"></default-action-ref> 不能和 *_*连用 -->
43 <action name="index">
44 <result>/admin/index.html</result>
45 </action>
46
47 <action name="*_*" class="cn.edu.nuc.action.{1}Action" method="{2}">
48 <result>/admin/{1}_{2}.jsp</result>
49 <result name="input">/admin/{1}_{2}.jsp</result>
50
51 <!-- 局部异常映射 //如有特殊需要,则如此 -->
52 <exception-mapping result="sqlException" exception="java.sql.SQLException"></exception-mapping>
53 <result name="sqlException">/sqlException.jsp</result>
54
55
56 <exception-mapping result="nullPointerException" exception="java.lang.NullPointerException"></exception-mapping>
57 <result name="nullPointerException">/NullPointerException.jsp</result>
58 </action>
59
60 </package>
61
62 </struts>

抱歉!评论已关闭.