jsp - JSF 1.x 通用异常处理

标签 jsp jsf exception-handling jsf-1.2

如果我的业务层出现异常(例如,我的 JDBC 连接 bean 中的 SQL 异常),我如何将它与自定义消息一起传播到全局 error.jsp页?

最佳答案

JSF 1.x 不提供这种类型的任何隐式错误处理,尽管您可以使用导航规则重定向到错误页面(假设表单发布)...

<navigation-case>
<description>
Handle a generic error outcome that might be returned
by any application Action.
</description>
<display-name>Generic Error Outcome</display-name>
<from-outcome>loginRequired</from-outcome>
<to-view-id>/must-login-first.jsp</to-view-id>
</navigation-case>

...或使用重定向...
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext
        .getRequestContextPath()
        + "/messages.faces");
extContext.redirect(url);

我建议查看 JSF specification更多细节。

可以根据需要将错误消息放置在请求范围/ session 范围/url 参数上。

假设一个 Servlet 容器,你可以使用通常的 web.xml error page configuration .
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errorPage.faces</location>
</error-page>

在你的支持 bean 中,你可以在 RuntimeException 中包装和抛出你检查过的异常。 s。

一些 JSF 实现/框架会捕获这些错误(Apache MyFaces/Facelets),因此您必须configure them not to .

关于jsp - JSF 1.x 通用异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/460596/

相关文章:

java - 使用 eclipse、maven、spring、hibernate、jsf 快速重新加载开发中的 java web 应用程序中的更改

jsf - org.apache.el.parser.ParseException : Encountered "(" at line X, 列 Y. 期待 [...]

c++ - 处理超出范围的索引

android - 处理 Android 自定义内容提供程序中的(跨进程)异常

css - 幻灯片图片高度调整

java - JSP onclick 事件在传递链接中更新的变量之前

java - ajax 更新时未显示消息

php - Laravel 5 覆盖异常处理程序

javascript - 另一个 JSP(也使用 jQuery)内的 JSP(使用 jQuery)出现问题

java - 添加外部 JAR 到 JDK