从 FacesContext 隐藏 JSP
我有一个 JSF 应用程序,其中的 Servlet Filter
配置为 /faces/*
的 urlPattern。 我想从 faces 上下文中隐藏 JSP,这样它就不会通过 Servlet Filter
。 ,我将其保存在项目的 WebContent
文件夹下,命名为 WebContent/Error.jsp
,并在 web.xml
中声明如下:
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/Error.jsp</location>
</error-page>
因此 Error.jsp
永远不会被拾取。相反,我看到 404 页面未找到错误。
更清楚地说,我希望我的 Error.jsp
页面 URL 为:
但只能通过以下方式访问:
一样的是当我在faces-config.xml中声明任何view-id
时的情况。如果我想在 faces
上下文中隐藏错误 JSP,我应该将其保存在哪里?
I have a JSF application with a Servlet Filter
configured for a urlPattern of /faces/*
.
I want to hide a JSP from faces context so that it won't go through the Servlet Filter
.
So I kept it under WebContent
folder of my project as WebContent/Error.jsp
and declared like the following in the web.xml
:
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/Error.jsp</location>
</error-page>
But my Error.jsp
never gets picked up. Instead I see 404 Page not found error.
To be more clear, I want my Error.jsp
page URL to be:
But it is only reachable by:
The same is the case when I declare any view-id
in the faces-config.xml
. Where do I keep the error JSP if I want to hide it from faces
context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据目前提供的信息,看起来应该可以正常工作。过滤器上没有
ERROR
,因此每当抛出 NPE 时根本不应该调用过滤器。显然,NPE 被另一个异常所包围,因为它被抛出到不合逻辑的地方,例如 bean 的构造函数,而不是正常的 bean 操作方法。在这种情况下,JSF 会将其作为
ManagedBeanCreationException
重新抛出。容器将获取它而不是 NPE,因此将无法找到错误页面。在容器的默认 HTTP 500 错误页面中,您应该读取堆栈跟踪的最顶层异常,以确定要为其定义错误页面的正确异常。请记住,像 NPE 这样的运行时异常是开发人员错误(错误!),而不是生产错误,应该尽快修复它们。我个人只会对此类错误使用全局 HTTP 500 错误页面:
对于更具体的、真实的生产异常,您始终可以声明更具体的错误页面:
Based on the information given so far, it looks as it should just work fine. You don't have a
<dispatcher>ERROR</dispatcher>
on the filter, so the filter should not be invoked at all whenever the NPE is thrown.Apparently the NPE got wrapped up in another exception because it's been thrown at an illogical place such as bean's constructor instead of a normal bean action method. In such case, JSF would rethrow it as a
ManagedBeanCreationException
. The container would get it instead of the NPE and thus won't be able to locate the error page. In the container's default HTTP 500 error page, you should read the topmost exception of the stacktrace in order to determine the right exception to define an error page for.Please keep in mind that runtime exceptions like NPEs are developer errors (bugs!), not production errors and that they should be fixed ASAP. I'd personally just use a global HTTP 500 error page for this kind of bugs:
For more specific, real production exceptions, you can always declare a more specific error page:
如果您定义了一个过滤器并在 web.xml 中声明了该过滤器,则所有请求都将通过该过滤器,除非您定义了过滤器映射。
我认为您可以在 web.xml 中定义一个过滤器映射,如下所示:
这未经测试,只是一个灵感。
编辑:您可以从Oracle站点了解更多信息
If you define a filter and declear that in your web.xml, all the request will go through that filter, unless you define the filter mapping.
I think you can define a filter mapping in your web.xml as following:
this is not tested, but just an inspiration.
edit: you can find out more from Oracle site
对我来说,面孔过滤器听起来与这个问题没有任何关系。您能否在完全禁用过滤器的情况下访问 http://localhost:8080/myappname/Error.jsp ?如果没有,那么 Error.jsp 文件本身可能存在一些问题?
如果没有看到代码本身,很难说。
To me it doesn't sound like the Faces filter has anything to do with this issue. Can you reach http://localhost:8080/myappname/Error.jsp with the filter completely disabled? If not, then perhaps there is some issue with the Error.jsp file itself?
It's hard to say without seeing the code itself.