从 FacesContext 隐藏 JSP

发布于 2024-12-11 20:50:39 字数 1064 浏览 0 评论 0原文

我有一个 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 为:

http://localhost:8080/myappname/Error.jsp

但只能通过以下方式访问:

http://localhost:8080/myappname/faces/Error.jsp< /p>

一样的是当我在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:

http://localhost:8080/myappname/Error.jsp

But it is only reachable by:

http://localhost:8080/myappname/faces/Error.jsp

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无妨# 2024-12-18 20:50:39

根据目前提供的信息,看起来应该可以正常工作。过滤器上没有 ERROR,因此每当抛出 NPE 时根本不应该调用过滤器。

显然,NPE 被另一个异常所包围,因为它被抛出到不合逻辑的地方,例如 bean 的构造函数,而不是正常的 bean 操作方法。在这种情况下,JSF 会将其作为 ManagedBeanCreationException 重新抛出。容器将获取它而不是 NPE,因此将无法找到错误页面。在容器的默认 HTTP 500 错误页面中,您应该读取堆栈跟踪的最顶层异常,以确定要为其定义错误页面的正确异常。

请记住,像 NPE 这样的运行时异常是开发人员错误(错误!),而不是生产错误,应该尽快修复它们。我个人只会对此类错误使用全局 HTTP 500 错误页面:

<error-page>
    <status-code>500<status-code>
    <location>/errors/generic.jsp</location>
</error-page>

对于更具体的、真实的生产异常,您始终可以声明更具体的错误页面:

<error-page>
    <exception-type>com.example.YourDatabaseAccessException</exception-type>
    <location>/errors/database.jsp</location>
</error-page>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/errors/sessionexpired.jsp</location>
</error-page>

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:

<error-page>
    <status-code>500<status-code>
    <location>/errors/generic.jsp</location>
</error-page>

For more specific, real production exceptions, you can always declare a more specific error page:

<error-page>
    <exception-type>com.example.YourDatabaseAccessException</exception-type>
    <location>/errors/database.jsp</location>
</error-page>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/errors/sessionexpired.jsp</location>
</error-page>
月野兔 2024-12-18 20:50:39

如果您定义了一个过滤器并在 web.xml 中声明了该过滤器,则所有请求都将通过该过滤器,除非您定义了过滤器映射。

我认为您可以在 web.xml 中定义一个过滤器映射,如下所示:

<filter> 
<filter-name>URLFilter</filter-name> 
<filter-class>the filter class in your source code</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>URLFilter</filter-name> 
<url-pattern>/some pages</url-pattern>//skip error.jsp here
</filter-mapping> 

这未经测试,只是一个灵感。
编辑:您可以从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:

<filter> 
<filter-name>URLFilter</filter-name> 
<filter-class>the filter class in your source code</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>URLFilter</filter-name> 
<url-pattern>/some pages</url-pattern>//skip error.jsp here
</filter-mapping> 

this is not tested, but just an inspiration.
edit: you can find out more from Oracle site

七七 2024-12-18 20:50:39

对我来说,面孔过滤器听起来与这个问题没有任何关系。您能否在完全禁用过滤器的情况下访问 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文