溫故知新:struts2_09其他功能:異常的處理

          針對運行時產生的異常,除了try/catch捕獲之后進行自定義處理,還可以考慮使用struts2提供的全局結果集。

          Action:
           1 package demo.action;
           2 
           3 public class HelloWorld {
           4 
           5     public String execute() throws Exception {
           6         //模擬一個異常
           7         if (true) {
           8             throw new Exception("Exception test");
           9         }
          10         return "success";
          11     }
          12     
          13 }
          14 
          struts.xml
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <!DOCTYPE struts PUBLIC
           3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
           4     "http://struts.apache.org/dtds/struts-2.3.dtd">
           5 <struts>
           6 
           7     <constant name="struts.devMode" value="true" />
           8     <package name="helloworld" extends="struts-default" namespace="/">
           9 
          10         <global-results>
          11             <!-- 指定一個頁面作為異常頁面 -->
          12             <result name="error">/error.jsp</result>
          13         </global-results>
          14 
          15         <global-exception-mappings>
          16             <!-- 配置需要捕獲的異常類型,以及返回結果 -->
          17             <exception-mapping result="error" exception="Exception" />
          18         </global-exception-mappings>
          19 
          20         <action name="hello" class="demo.action.HelloWorld">
          21             <result name="success">/helloWorld.jsp</result>
          22         </action>
          23 
          24     </package>
          25 
          26 </struts> 
          全局異常結果集的配置很簡便,可以配置一些404或者一些通用的異常頁面,在頁面中使用${exception.message}就可以獲得異常信息。

          posted on 2014-11-03 10:37 都較瘦 閱讀(101) 評論(0)  編輯  收藏 所屬分類: MVCFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平远县| 汝阳县| 科尔| 宿州市| 灯塔市| 大足县| 新宾| 象山县| 句容市| 梅河口市| 桐柏县| 班戈县| 雷州市| 兴海县| 金塔县| 柯坪县| 固原市| 黄龙县| 临武县| 崇明县| 中卫市| 陆河县| 静乐县| 清涧县| 海林市| 汉阴县| 拜泉县| 濮阳市| 留坝县| 大安市| 麻城市| 酉阳| 岳西县| 钦州市| 宣威市| 噶尔县| 鄯善县| 满城县| 囊谦县| 红河县| 子洲县|