struts異常信息解讀

異常,是基本上每個系統都會出現

   這裏講點struts的異常信息的用法,很簡單的一個實例

     首先自己定義個異常類,我這裏定義了一個運行時異常類

package com.asiainfo.struts.exception;

public class MyException extends RuntimeException{

	public MyException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MyException(String message, Throwable cause,
			boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

	public MyException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public MyException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public MyException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}
	 

}

配置struts.xml文件
<global-results> 
        <result name="exception">WEB-INF/exception/error.jsp</result>
      </global-results>
      
      <global-exception-mappings>
        <exception-mapping result="exception" exception="com.asiainfo.struts.exception.MyException"></exception-mapping>
      </global-exception-mappings>

error.jsp

  

<body>
    ${exception.message }
  </body>

然後自己寫一個運行時的異常,讓其拋出
  public String validator(){
    	 int i=10;
    	 if(i==10){
    		throw new MyException("出現異常");
    	 }
    	 return "success";
     }

這時,strust.xml的異常配置會捕捉action裏面拋出的自定義異常信息

   然後將其傳遞給error.jsp

發佈了36 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章