Struts(六)struts2的異常處理與全局異常與結果


1.exception一般都繼承Exception

例子:

usernameException.class

package com.liule.exception;

public class usernameException extends Exception
{
        private String message;//提示消息
        
        public usernameException(String message)
        {
            super(message);
            
            this.message = message;
        }

        public String getMessage()
        {
            return message;
        }

        public void setMessage(String message)
        {
            this.message = message;
        }
    
}

LoginAction.java(validate一般用於無業務邏輯的)

public String execute() throws Exception
    {
        if(!"hello".equals(username))
        {
            throw new usernameException("username validate!");
        }

        return SUCCESS;
    }

struts.xml(局部異常)

<action name="login" class="com.liule.action.LoginAction">
            <exception-mapping exception="com.liule.exception.usernameException" result="usernameerror"></exception-mapping>
            <result name="success">/servlet.jsp</result>
            <result name="usernameerror">/usernameerror.jsp</result>
        </action>

全局結果(每一個action如果返回這個異常,都可以調用這個結果)

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

 

對於struts.xml文件的結果配置來說,局部優於全局。(就是在全局與局部都存在的情況下,執行局部)

 

全局異常

<global-exception-mappings>
            <exception-mapping result="com.liule.exception.usernameException" exception="usernameerror"></exception-mapping>
        </global-exception-mappings>

 

我們既可以在action中定義異常與結果,也可以定義全局的異常與結果,局部是優於全局的,如果定義成全局,那么可以為所有的action所公用,而局部的異常與結果只能被當前的action所獨享,不能為其他action所共享。

 

http://www.cnblogs.com/codeplus/archive/2011/07/16/2107999.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM