jsf - FacesExceptionFilter OmniFaces 使用 WEB-INF 内的错误页面

标签 jsf web.xml omnifaces

我需要一些使用 OmniFaces 功能的帮助。

我尝试使用 FacesExceptionFilter 在遇到异常时将用户重定向到错误页面。

我有以下 web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>

<display-name>xxxxx</display-name>

<session-config>
    <session-timeout>1</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>pages/secured/main.xhtml</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-application-context.xml</param-value>
</context-param>

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
    <param-value>2</param-value>
</context-param>

<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>mytheme</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.SEPARATOR_CHAR</param-name>
    <param-value>_</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>com.bdo.corpsoa.utilities.security.listeners.impl.CorpSoaSessionListener</listener-class>
</listener>

<filter>
    <filter-name>facesExceptionFilter</filter-name>
    <filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>facesExceptionFilter</filter-name>
    <servlet-name>facesServlet</servlet-name>
</filter-mapping>


<context-param>
    <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATH</param-name>
    <param-value>/*.xhtml</param-value>
</context-param>

<context-param>
    <param-name>org.omnifaces.FACES_VIEWS_PATH_ACTION</param-name>
    <param-value>REDIRECT_TO_SCANNED_EXTENSIONLESS</param-value>
</context-param>

<!-- <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> -->

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/WEB-INF/pages/errors/viewExpired.xhtml</location>
</error-page>

<error-page>
    <exception-type>java.lang.RuntimeException</exception-type>
    <location>/WEB-INF/pages/errors/unHandled.xhtml</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/errors/pageNotFound.xhtml</location>
</error-page>

<error-page>
    <exception-type>java.io.FileNotFoundException</exception-type>
    <location>/WEB-INF/pages/errors/pageNotFound.xhtml</location>
</error-page>

但是,当发生异常时,用户只会重定向到默认的 500 错误页面。

仅当我将错误页面移出 WEB-INF 文件夹时,重定向才会起作用 /WEB-INF/pages/errors/pageNotFound.xhtml -->/pages/errors/pageNotFound.xhtml

但这意味着现在可以直接访问错误页面。但在 Omnifaces 展示中这应该是可能的。我不知道我错过了什么,请帮忙...

这是 Omnifaces 展示的 web.xml,它显示 WEB-INF 中可能存在错误页面。

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 元数据完整=“假”

    <display-name>OmniFaces Showcase</display-name>


    <!-- Standard JSF settings. -->

    <context-param>
            <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
            <param-value>65535</param-value> <!-- 64KB. -->
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value> <!-- Should be -1 for production. -->
    </context-param>
    <context-param>
            <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
            <param-value>/WEB-INF/showcase.taglib.xml</param-value>
    </context-param>
    <context-param>
            <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
            <param-value>true</param-value>
    </context-param>


    <!-- Mojarra/RI specific settings. -->

    <context-param>
            <param-name>com.sun.faces.defaultResourceMaxAge</param-name>
            <param-value>3628800000</param-value> <!-- 6 weeks. -->
    </context-param>


    <!-- MyFaces specific settings. -->

    <context-param>
            <param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
            <param-value>3628800000</param-value> <!-- 6 weeks. -->
    </context-param>
    <context-param>
            <!--
                    MyFaces and Mojarra don't agree on the default setting for actually serializing state
                    in the session as opposed to just storing a reference. Mojarra's default is false, but
                    can be switched to true. MyFaces' default is true, and can be switched to false, which
                    we thus do below. See http://arjan-tijms.omnifaces.org/p/jsf-22.html#1127
             -->
            <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
            <param-value>false</param-value>
    </context-param>


    <!-- OmniFaces specific settings. -->

    <context-param>
            <param-name>org.omnifaces.CACHE_SETTING_SESSION_MAX_CAPACITY</param-name>
            <param-value>6</param-value>
    </context-param>
    <context-param>
            <!--
                    All files in the 3 paths defined below will be scanned and made available
                    as extensionless JSF views. Since no explicit extension is given for scanning,
                    these paths should contain ONLY JSF (Facelets) files.
             -->
            <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
            <param-value>/showcase,/etc,/demo</param-value>
    </context-param>
    <context-param>
            <!--
                    Redirects the faces views scanned /showcase/[PAGE].xhtml to /[PAGE].
                    A 404 would normally be preferred (and this is thus the default), but the showcase app
                    already has published /showcase/[PAGE].xhtml
            -->
            <param-name>org.omnifaces.FACES_VIEWS_PATH_ACTION</param-name>
            <param-value>REDIRECT_TO_SCANNED_EXTENSIONLESS</param-value>
    </context-param>


    <!-- Servlets and filters. -->

    <servlet>
            <servlet-name>facesServlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
            <servlet-name>facesServlet</servlet-name>
            <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.omnifaces.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
            <filter-name>facesExceptionFilter</filter-name>
            <filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>facesExceptionFilter</filter-name>
            <servlet-name>facesServlet</servlet-name>
    </filter-mapping>

    <filter>
            <filter-name>gzipResponseFilter</filter-name>
            <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>gzipResponseFilter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>ERROR</dispatcher>
    </filter-mapping>


    <!-- Welcome files, error pages and mime types. -->
    
    <session-config>
            <cookie-config>
                    <http-only>true</http-only>
            </cookie-config>
            <tracking-mode>COOKIE</tracking-mode>
    </session-config>

    <welcome-file-list>
            <!--
                    Note that an extension is used here, since the index file resides within the
                    root which has not been configured for FacesViews scanning.
            -->
            <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <error-page>
            <exception-type>javax.faces.application.ViewExpiredException</exception-type>
            <location>/WEB-INF/errorpages/expired.xhtml</location>
    </error-page>
    <error-page>
            <exception-type>java.sql.SQLException</exception-type>
            <location>/WEB-INF/errorpages/database.xhtml</location>
    </error-page>
    <error-page>
            <exception-type>java.lang.RuntimeException</exception-type>
            <location>/WEB-INF/errorpages/bug.xhtml</location>
    </error-page>
    <error-page>
            <error-code>500</error-code>
            <location>/WEB-INF/errorpages/500.xhtml</location>
    </error-page>
    <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/errorpages/404.xhtml</location>
    </error-page>

    <mime-mapping>
            <!--
                    Silence WebLogic's annoying "JSF1091: No mime type could be found for file" warning.
            -->
            <extension>xhtml</extension>
            <mime-type>text/html</mime-type>
    </mime-mapping>

总结

  1. 如果在 WEB-INF 之外,则可以重定向到错误页面。
  2. 展示表明这应该是可能的。
  3. 我需要帮助......

感谢您的宝贵时间

编辑

似乎“如果在 WEB-INF 之外,重定向到错误页面就可以工作。”并不总是正确的。

如果在 WEB-INF 之外,将显示此错误页面

   <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:body>
    <h1>#{"XXXXXXXX"}</h1>

</h:body>
</html>

即使在 WEB-INF 之外也不会显示此错误页面。这将导致默认的 404 页面

 <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">
    
    <h:body>
        <h1>ZZZZZZZZZ</h1>
    
    </h:body>
    </html>

最佳答案

如果我在 jsf 页面上放置 EL 表达式或 jsf 组件,FacesExceptionFilter 似乎实际上正在工作。

由于某种原因,此错误页面将不起作用,并会生成默认的 404 页面。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:body>
    <h1>ZZZZZZZZZ</h1>
</h:body>
</html>

但是这个错误页面仍然有效。

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:body>
    <h1>#{"XXXXXXXX"}</h1>
</h:body>
</html>

关于jsf - FacesExceptionFilter OmniFaces 使用 WEB-INF 内的错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583607/

相关文章:

java - 如何为 f :selectItems? 中的枚举创建和使用通用 bean

java - 将 jsf web.xml 中的页面名称与文件名解耦

jsp中web.xml中的Java web应用程序全局参数

jsf - Omnifaces CombinedResourceHandler——有没有办法抑制某些资源?

jsf - 如何使用JSF H :outputStylesheet?链接外部CSS资源

css 选择器 java server faces 标签

jsf - FullAjaxExceptionHandler 不会在 ajax 按钮上显示 session 过期错误页面

jsf - 修复 Tomcat 启动时 OmniFaces BeanManager 初始化问题

jquery - PrimeFaces 中数据表内的按钮背景颜色/文本更改

java - web.xml 中的内容辅助