• 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

  • 相关阅读:
    谈谈node(1)
    怎么调用html5的摄像头,录音,视频?
    es6-块级作用域let 和 var的区别
    输入手机号自动分隔
    How do I know which version of Javascript I'm using?
    PHP的类中的常量,静态变量的问题。
    【转】马拉松式学习与技术人员的成长性
    JavaScript Prototype in Plain Language
    Promise编程规范
    XMLHttpRequest对象解读
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4941330.html
Copyright © 2020-2023  润新知