struts2 进阶--异常捕获机制

在SpringMvc中有自己的异常处理机制,struts2当然会有此功能,主要是在struts.xml中配置:

<bean type="com.opensymphony.xwork2.ActionEventListener" class="com.cml.action.listener.MyActionEventListener" />

class为我们自己实现的bean,自己实现的类需要实现接口:ActionEventListener

有了异常捕获机制后,就可以在action中抛出异常后进行对应的逻辑操作或是系统日志记录!

主要代码:


public class MyActionEventListener implements ActionEventListener
{


public MyActionEventListener()
{
log.debug("Am a new ActionListener!");
}


private Logger log = Logger.getLogger("root");

//抛出异常后调用此方法
public String handleException(Throwable t, ValueStack stack)
{
if (t instanceof RuntimeException)
{
// 进行需要的处理
log.debug("action抛出异常啦!"+stack.findString("username"));
}
log.debug("抛出异常!");
return "input";
}


@Override
public Object prepare(Object target, ValueStack stack)
{
log.debug("获取stack值:" + stack.findString("name") + ":"
+ target.getClass().getName());
System.out.println("获取stack值:" + stack.findString("username"));
return target;
}
}

 ValueStack stack为struts2的值栈!

target是访问的action对象!在prepare方法中可以为我们的action类添加一些业务逻辑或判断信息!


转载于:https://www.cnblogs.com/cmlblog/p/7663170.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值