【问题标题】:URL mapping of /test to /home/index.jsf only results in 404/test 到 /home/index.jsf 的 URL 映射仅导致 404
【发布时间】:2014-10-29 23:12:52
【问题描述】:

我一直在尝试为 JSF 2.2 设置 OCPsoft 的 Rewrite http://ocpsoft.org/rewrite/examples/

我的设置:

  • JSF 2.2
  • 雄猫 7
  • Servlet 容器 3.0
  • Maven 项目
  • URLRewrite 版本 2.0.12

我的 pom.xml 包含以下两个依赖项

<!-- for JSF 2.x -->
    <dependency>
        <groupId>org.ocpsoft.rewrite</groupId>
        <artifactId>rewrite-servlet</artifactId>
        <version>2.0.12.Final</version>
    </dependency>
    <dependency>
        <groupId>org.ocpsoft.rewrite</groupId>
        <artifactId>rewrite-config-prettyfaces</artifactId>
        <version>2.0.12.Final</version>
    </dependency>

我在我的 WEB-INF 文件夹中放置了一个 pretty-config.xml

    <pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">

    <url-mapping id="test">
        <pattern value="/test" />
        <view-id value="/home/index.jsf" />
    </url-mapping>

</pretty-config>

我的 web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>myproject</display-name>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
        <param-value>primefaces:jquery/jquery.js=http://code.jquery.com/jquery-1.11.0.min.js</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <description>State saving method: "client" or "server" (= default) 
        See JSF Specification 2.5.2. Zur Produktion wieder auf server schalten. HTTP 
        GET Request zusammen mit 'client'
    führen zum Verlust des STATE</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <description>Next step - disable compression of state in server</description>
        <param-name>org.apache.myfaces.COMPRESS_STATE_IN_SESSION</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <description>If true, rendered HTML code will be formatted, so that 
            it is "human readable". i.e. additional line separators and whitespace will 
            be written, that do not influence
        the HTML code. Default: "true"</description>
        <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <description>Very important, too, is to disable the serialization 
        of state, serialization and deserialization of the component tree is a
        major
        performance hit
    </description>
        <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
        <param-value>false</param-value>
    </context-param>
    <!-- Verschlüsselung des Viewstates -->
    <context-param>
        <param-name>org.apache.myfaces.secret</param-name>
        <param-value>NzY1NDMyMTA=</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.numberOfViewsInSession</param-name>
        <param-value>3</param-value>
    </context-param>

    <context-param>
        <param-name>com.sun.faces.numberOfLogicalViews</param-name>
        <param-value>10</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/error/404.jsf</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/home/index.jsf</location>
    </error-page>

    <filter>
        <filter-name>adminPinFilter</filter-name>
        <filter-class>com.myproject.filters.AdminPinFilter</filter-class>
    </filter>
    <filter>
        <filter-name>facesRedirectFilter</filter-name>
        <filter-class>com.myproject.filters.FacesRedirectFilter</filter-class>
        <init-param>
            <description>Auf diese Extension soll nicht direkt zugegriffen
                    werden
                    (z.B. .xhtml)</description>
            <param-name>forbiddenExtension</param-name>
            <param-value>.xhtml</param-value>
        </init-param>
        <init-param>
            <description>Auf diese Extension soll umgeleitet werden (z.B. .jsf)</description>
            <param-name>redirectExtension</param-name>
            <param-value>.jsf</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>directGetFilter</filter-name>
        <filter-class>com.myproject.filters.DirectGetFilter</filter-class>
    </filter>
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter>
        <filter-name>sessionFilter</filter-name>
        <filter-class>com.myproject.filters.SessionFilter</filter-class>
    </filter>
    <filter>
        <filter-name>authFilter</filter-name>
        <filter-class>com.myproject.filters.AuthFilter</filter-class>
    </filter>
    <filter>
        <filter-name>offerFilter</filter-name>
        <filter-class>com.myproject.filters.OfferFilter</filter-class>
    </filter>
    <filter>
        <filter-name>adminAuthFilter</filter-name>
        <filter-class>com.myproject.filters.AdminAuthFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <filter-mapping>
        <filter-name>adminPinFilter</filter-name>
        <url-pattern>/adminPin/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>facesRedirectFilter</filter-name>
        <url-pattern>*.xhtml</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sessionFilter</filter-name>
        <url-pattern>*.jsf</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>authFilter</filter-name>
        <url-pattern>/profile/*</url-pattern>
        <url-pattern>/conversation/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>adminAuthFilter</filter-name>
        <url-pattern>/admin/panel/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>offerFilter</filter-name>
        <url-pattern>/offer/view/*</url-pattern>
    </filter-mapping>

    <session-config>
        <!-- Disables URL-based sessions (no more 'jsessionid' in the URL using 
            Tomcat) -->
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>

    <listener>
        <listener-class>com.myproject.system.ContextFinalizer</listener-class>
    </listener>
</web-app>

但是,如果我导航到 /test,我只会收到 404 错误。

有什么我错过的吗?

【问题讨论】:

    标签: jsf jsf-2 url-rewriting prettyfaces


    【解决方案1】:

    您的网络应用程序中是否有一个名为“/home/index.xhtml”的文件(或类似的东西)?如果可能,还请发布您的 web.xml。谢谢!

    此外,您的 web.xml 中似乎定义了很多过滤器,但您没有定义重写过滤器。这应该没问题,但是,值得手动添加过滤器并看看会发生什么。重写过滤器应该是 web.xml 中的第一个过滤器:

    <listener>
       <listener-class>org.ocpsoft.rewrite.servlet.impl.RewriteServletRequestListener</listener-class>
    </listener>
    
    <listener>
       <listener-class>org.ocpsoft.rewrite.servlet.impl.RewriteServletContextListener</listener-class>
    </listener>
    
    <filter>
       <filter-name>OCPsoft Rewrite Filter</filter-name>
       <filter-class>org.ocpsoft.rewrite.servlet.RewriteFilter</filter-class>
       <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
       <filter-name>OCPsoft Rewrite Filter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
       <dispatcher>ASYNC</dispatcher>
       <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    

    【讨论】:

    • 编辑了我上面的帖子,是的,我的主目录中有一个 index.xhtml
    • 编辑了我的答案,再试一次?
    • 在上面添加了过滤器和监听器,没有变化。 AFAIK,在使用 servlet 容器 3.0 时,最新版本的重写不需要额外的过滤器。
    • 没错,我只是想确保顺序正确。嗯...您认为您可以在支持论坛上发帖并附上重现此问题的示例应用程序(或上传并在其他地方共享链接)吗?我应该能够在应用程序上提供更多帮助:ocpsoft.org/support(如果您在创建帐户时遇到问题,请告诉我。)
    【解决方案2】:

    现在一切正常。我从 web.xml 中删除了默认 servlet。

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    

    【讨论】:

      猜你喜欢
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多