在struts.xml中声明/处理哪些异常 [英] Which Exceptions to declare/handle in struts.xml

查看:52
本文介绍了在struts.xml中声明/处理哪些异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中有 Checked Unchecked异常.

由于我是 Struts2 的新手,因此我对处理已检查的异常有一个愚蠢的疑问.我们编写 try catch块来处理已检查的异常.因此,我们可以在 catch块 return视图页面名称(显示 Exception / Error )的页面名称中编写return语句.

I have one silly doubt regarding handling checked exception in mind as I am new to Struts2. We write try catch block to handle checked exceptions. So, we can write return statement in catch block and return view name of page which shows Exception/Error.

如果我抛出在 struts.xml 文件中声明的 checked exception ,结果是重定向到 Error /例外页面?

And what if I throw checked exception which is declared in struts.xml file and a result is a redirect to a Error/Exception page?

两者都是检查异常的正确方法吗?

推荐答案

应捕获应用程序级别的异常.如果抛出应用程序级异常但未捕获,则

The application level exceptions should be caught. If you throw an application level exception and didn't catch it, it could be caught by the exception interceptor if it's configured to the action.

注意,异常拦截器位于 defaultStack 的底部,该异常拦截器不仅可以捕获动作中的异常,还可以捕获整个拦截器堆栈中的异常.如果您将此拦截器配置为映射异常,则它应处理并在其后返回结果(也应将其配置为全局结果),然后将捕获您的异常并返回结果.如果没有,则Web容器将处理该异常.正确的方法是捕获异常并返回错误结果,但是如果无法执行此操作,则应配置异常拦截器.

Note, the exception interceptor is on bottom of the defaultStack that allows to catch exceptions not only in the action but in the whole stack of interceptors. If you configured this interceptor to map exceptions it should handle and result returned after it, which also should be configured as global results, then your exception will be caught and result returned. If you didn't, the web container will handle the exception. The right way is to catch exception and return an error result, but if you can't do it then you should configure exception interceptor.

注意,在代码中发生的Struts容器异常不会由异常拦截器处理.在这种情况下,您应该捕获异常并将操作的状态设置为错误,然后可以在操作执行中首先检查该状态以返回错误结果或引发代码中异常拦截器可能捕获的异常无法返回错误结果的地方.您可以在文档中找到有关异常配置的更多信息.

Note, that Struts container exceptions occurred in your code aren't handled by the exception interceptor. In this case you should catch the exception and set the state of the action to error and after that you can check that state first in the action execution to return an error result or throw an exception that could be caught by the exception interceptor in the code where you can't return an error result. More about exception configuration you can find in the documentation.

这篇关于在struts.xml中声明/处理哪些异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆