zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    搭建环境遇到的几个问题
    webservice
    Eclipse 反编译 阅读class 文件
    设置navigationBar上面的item
    自定义的UITabbar上面的按钮的x坐标的计算方法
    UIToolbar自定义背景及按钮设置
    UITabBar实现自定义背景及UITabBarItem自定义图片和字体
    IOS APP圆形图片的实现
    如果AlertView输入框为空,则禁止点击确定按钮
    两种局部刷新UITableView的方法的使用条件
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4941330.html
Copyright © 2011-2022 走看看