在 struts.xml 中声明/处理哪些异常

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

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

问题描述

Java中有CheckedUnchecked exceptions.

由于我是 Struts2 的新手,所以我对处理 checked exception 有一个愚蠢的疑问.我们编写try catch block来处理checked exceptions.因此,我们可以在 catch blockreturn view 页面的名称中编写返回语句,显示 Exception/Error.

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?

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

推荐答案

应捕获应用程序级异常.如果你抛出一个应用程序级异常并且没有捕获它,它可能会被 exception 拦截器 如果它被配置为动作.

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 中声明/处理哪些异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 struts.xml 中声明/处理哪些异常

基础教程推荐