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

 

转载于:https://www.cnblogs.com/liu-Gray/p/4941330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值