Struts2的异常处理

Struts2的异常处理

1.异常处理机制(1)发送请求到控制器(Action);

(2)Action出现异常后,依照所捕捉的不同异常转入不同的视图资源。

2.异常捕捉

(1)在Action的处理逻辑中,手动捕捉异常,在捕捉到特定的异常后,返回指定的逻辑视图名,这种方式太繁琐不利于代码的修改和维护:

import com.opensymphony.xwork2.ActionSupport;

public class Test3Action extends ActionSupport{
    //逻辑视图名
    static final String ERROR_1 = "error1";
    //封装一个请求参数
    private String username;
    //setter、getter方法
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return this.username;
    }
    //处理逻辑,在此处我们部直接抛出异常而是用try...catch的方式手动捕捉异常
    //根据不同的异常返回不同的逻辑视图名
    public String execute(){
        try{
            return SUCCESS;
        }catch(Exception e){
            return ERROR_1;
        }
    }
}

(2)声明式的异常捕捉:

  Action的处理逻辑抛出所有的异常给Struts2框架,当Struts2框架接收到所抛出的异常后,根据在Struts.xml中配置的异常映射转入不同的物理视图资源。

  这种异常处理是通过在Struts.xml中配置<exception-mapping>完成的,使用该元素需要指定两个属性:

  exception:指定该异常映射所设置的异常类型;

  result:指定Action出现异常时系统返回的逻辑视图名。

  根据<exception-mapping>元素出现的位置不同,我们可以把异常分为两种:局部异常(在<action>中配置)和全局异常(在<global-exception-mapping>中配置),局部异常对该Action有效,而全局异常对所有Action有效,当全局异常和局部异常配置了同一个异常类型时,在此Action中局部异常会覆盖全局异常。

自定义两个异常类:

UserException:

package exception;

public class UserException extends Exception{
    public UserException(){

    }
    public UserException(String msg){
        super(msg);
    }
}

ErrorException:

package exception;

public class ErrorException extends Exception{
    public ErrorException(){

    }
    public ErrorException(String msg){
        super(msg);
    }
}

写一个抛出异常的Action:

import com.opensymphony.xwork2.Action;
import exception.ErrorException;import exception.UserException;
public class Test4Action implements Action{
    //封装请求参数
    private String username;
    //setter、getter方法
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return this.username;
    }

    @Override
    public String execute() throws Exception {
        if(getUsername()==null){
            throw new UserException("用户名不得为空");
        }else if(!getUsername().equals("jiagoushi")){
            throw new ErrorException("用户名错误");
        }else{
            return SUCCESS;
        }
    }

}

在struts.xml中配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="package_a" extends="struts-default">
        <!-- 定义一个全局结果 -->
        <global-results>
            <result name="errorException">errorException.jsp</result>
        </global-results>
        <!-- 定义一个全局异常 -->
        <global-exception-mappings>
            <!-- 当Action遇到ErrorException异常时,将转入到名为errorException的结果中 -->
            <exception-mapping result="errorException" exception="exception.ErrorException"/>
        </global-exception-mappings>
        <action name="test1" class="testAction.Test1Action">
            <result name="error">error.jsp</result>
            <result>${name}.jsp</result>
        </action>
        <action name="test4" class="testAction.Test4Action">
            <!-- 定义局部异常处理 -->
            <exception-mapping result="userException" exception="exception.UserException"/>
            <exception-mapping result="errorException" exception="exception.ErrorException"/>
            <!-- 定义结果映射 -->
            <result>welcome.jsp</result>
            <result name="userException">userException.jsp</result>
            <!-- 局部异常会覆盖全局异常 -->
            <result name="errorException">errorException_test.jsp</result>
        </action>
    </package>
</struts>

3.异常信息的输出使用Struts2提供的标签可以用来输出异常信息:

<s:property value="exception">:输出异常对象;

<s:property value="exceptionStack">:输出异常堆栈信息。

时间: 2024-12-30 04:01:23

Struts2的异常处理的相关文章

【struts2】Struts2的异常处理

在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception. 1)自己实现异常处理 我们还以helloWorldAction为例,在Action的execute方法中这样写: public String execute() throws Exception { int a=5/0; this.businessExecute(); return "toWelcome"; }

(十)struts2的异常处理机制

成熟的MVC框架应该提供成熟的异常处理机制.当然可以在方法中手动捕捉异常,当捕捉到特定异常时,返回特定逻辑视图名. 这种方式非常繁琐,需要在方法中写大量try catch块,最大的缺点还是一旦需要改变异常处理方法时,需要修改代码. 最好的方式是通过声明式的方式管理异常处理.struts2提供了一种声明式的异常处理方式. 一.原理 我们看Action接口中的execute方法声明. public String execute() throws Exception 这就意味着我们重写该方法时,无需进

java之struts2之异常处理

1.在应用项目中,异常的出现时很正常的.而且项目上线后发生异常也很正常的.那么需要对这些异常有相应的处理机制,以便客户能够看你到更加友好的界面.Struts2中提供了异常处理机制. 2.Struts中异常处理实现 a) 在 action 处理类中抛出的异常 public class UserAction { public String add() throws NullPointerException{ if(1==1) throw new NullPointerException("数据为nu

Struts2全局异常处理

1.在struts.xml中配置全局异常处理 在Action中抛出异常,此异常可以是action自己抛的,也可以是Service抛出来的,都会跳转到全局异常中,只有在当前Action中配置域全局异常返回的result中name相同的result,就能跳转到指定错误视图 并在struts.xml中对应Action中配置跳转的error视图: JSP中通过Struts标签并结合OGNL表达式从值栈中获取懂啊异常信息显示出来,如果不知道值栈中异常对象的名称可以先<s:debugger>看一下,如:

Struts2(六)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; } p

Struts2 语法--异常处理

1. UsersDAO.java里产生一个例外: System.out.println(1/0); 2. 调用DAO的UsersAction1.java 的execute方法, 加抛异常: public String execute() throws Exception 3. struts.xml配置, <global-results> <result name="login_success">/login_success.jsp</result>

struts2异常处理机制

一.处理一般异常(javaBean异常) struts2进行异常处理首先需要添加exception拦截器,而默认拦截器栈已经加入了这个拦截器,所以不用特意的声明.在Struts 2框架中,采用声明式异常处理方式.在这种方式下,只需要在struts.xml文件中进行配置,Struts 2便能够处理异常,并跳转到相应的视图,而在Action中无须编写任何异常处理代码. 如果Action在处理请求的过程中出现异常,一个名称为exception的拦截器将拦截该异常,并进行处理.所以在struts.xml

Struts2中的异常处理

因为在Action的execute方法声明时就抛出了Exception异常,所以我们无需再execute方法中捕捉异常,仅需在struts.xml 中配置异常处理. 为了使用Struts2的异常处理机制,必须打开Struts2的异常映射功能,这需要exception拦截器.在struts-default.xml文件中已经开启了exception拦截器. 声明式异常捕捉 Struts2的异常处理机制是通过在struts.xml文件中配置<exception-mapping--/>元素完成的,配置

Struts2之配置文件中Action的详细配置(续)

承接上一篇 4.处理结果的配置 Action类的实例对象调用某个方法,处理完用户请求之后,将返回一个逻辑视图名的字符串.核心Filter收到返回的逻辑视图名字符串,根据struts.xml中的逻辑视图名与物理视图名的对应关系,通过地址转发 ,转发到物理视图中去. 配置处理结果,通过使用<result>元素,放在<action>元素下面. <result   name=“”   type=" ">    </result> Struts2支