溫故知新: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

          導航

          統計

          公告

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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 娄底市| 东乡| 安图县| 诏安县| 兴国县| 高陵县| 内江市| 鄂托克旗| 渝中区| 平罗县| 高邑县| 建瓯市| 扎赉特旗| 阜南县| 舟山市| 瓦房店市| 仙居县| 延吉市| 阳原县| 昌都县| 江北区| 呼伦贝尔市| 崇左市| 长海县| 蓬安县| 太和县| 石河子市| 东方市| 龙井市| 南川市| 阿拉善盟| 奇台县| 宁武县| 渝中区| 甘泉县| 时尚| 肇东市| 汕头市| 南宁市| 成都市| 桂林市|