如果我的业务层出现异常(例如,我的 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/