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

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

问题描述

在Java中有已检查未检查的例外

我有一个怀疑处理 checked exception ,因为我是新的 Struts2 。我们写 try catch block 来处理 checked exceptions 。所以,我们可以在 catch块中写返回语句异常 / 错误

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 文件中声明的异常,结果是重定向到错误 / 例外页?

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?

推荐答案

应该捕获应用程序级异常。如果你抛出一个应用程序级异常并没有捕获它,它可能被 exception 拦截器捕获,如果它被配置为操作。注意,异常拦截器在 defaultStack 的底部,允许捕获异常不仅在操作中,而且在整个拦截器堆栈中。如果你配置这个拦截器来映射异常,它应该处理并返回结果,它也应该配置为全局结果,那么你的异常将被捕获并返回结果。如果没有,web容器将处理异常。正确的方法是捕获异常并返回错误结果,但如果你不能做,那么你应该配置异常拦截器。注意,在您的代码中发生的struts容器异常不会由异常拦截器处理。在这种情况下,应该捕获异常并将操作的状态设置为错误,然后可以在操作执行中首先检查该状态以返回错误结果或抛出可能由代码中的异常拦截器捕获的异常其中不能返回错误结果。有关异常配置的详情,请参阅文档。

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. 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. 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天全站免登陆