JSF2.x:子文件夹中的页面无法访问

时间:2014-05-23 15:25:12

标签: java jsf-2

这是JSF2.1,JBoss 6 EAP7.1服务器项目

我想设置以下内容:

  1. 用户在浏览器中输入此网址:http://domain.com/webApp/pathA
  2. 服务器检测到请求并注意/ pathA位
  3. JSF将浏览器重定向到 domain.com/webApp/pathB/b.xhtml
  4. 过滤器类正确运行,它由/ pathA触发并运行response.sendRedirect(contextPath +" /pathB/b.xhtml");

    但浏览器遇到错误:

    [地址栏] domain.com/webApp/pathB/b.xhtml

    发生错误:

    /b.xhtml在ExternalContext中找不到作为资源

    com.sun.faces.context.FacesFileNotFoundException:/b.xhtml在ExternalContext中找不到资源     at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232)     at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273)     at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:20

    如果我直接在浏览器中输入domain.com/webApp/pathB/b.xhtml,我会收到相同的错误消息

    我想问题是在web.xml中,但我看不出为什么......你能帮帮我吗?

    的web.xml

        <!-- Change to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    
    
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    
    <filter>
        <filter-name>FormFilter</filter-name>
        <filter-class>com.kit.filter.FormFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>FormFilter</filter-name>
        <url-pattern>/pathA/*</url-pattern>
    </filter-mapping>
    
    <context-param>
         <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
     </context-param>
    
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- Map these files with JSF -->
    <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
      <url-pattern>/pathB/*</url-pattern>
    </servlet-mapping>
     </web-app>
    

    FormFilter.java

    package com.kit.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class FormFilter implements Filter{
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
            HttpServletRequest req = (HttpServletRequest) request;
            //HttpSession session = req.getSession();
    
            System.out.println("---------------------------------------------");
            System.out.println("req.getRequestURI()=" + req.getRequestURI());
            System.out.println("req.getContextPath()=" + req.getContextPath());
            System.out.println("req.getRequestURL()=" + req.getRequestURL());
            System.out.println("---------------------------------------------");
    
            if (req.getRequestURI().contains("pathA"))
            {
                //HttpServletResponse res = (HttpServletResponse) response;
                //res.sendRedirect("pathA/helloWorldA.xhtml");
    
                String contextPath = ((HttpServletRequest)request).getContextPath();
    
                System.out.println("~~~~~~~~~~~~~~ redirect to: " + contextPath + "/pathB/b.xhtml" );
    
                ((HttpServletResponse)response).sendRedirect(contextPath + "/pathB/b.xhtml");
    
            }
            else
            {
                chain.doFilter(request, response);
            }
    
    
        }
    
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        public void destroy() {
        }
    
    
    }
    

    a.xhtml(b.xhtml是一个类似于a.xhtml的虚拟页面)

    <h:head>
        <title>Secured welcome page</title>
    </h:head>
    <h:body>
        <h:form>
            <h3>Welcome AAAA</h3>
            <br/>
            <h:outputText value="This is an info page. Everyone has access to it!"/>
            <br/>
    
        </h:form>
    </h:body>
    </html>
    

    文件结构

    F:.
    │  .classpath
    │  .project
    │  pom.xml
    │
    ├─.settings
    │      .jsdtscope
    │      org.eclipse.jdt.core.prefs
    │      org.eclipse.m2e.core.prefs
    │      org.eclipse.wst.common.component
    │      org.eclipse.wst.common.project.facet.core.pref
    │      org.eclipse.wst.common.project.facet.core.xml
    │      org.eclipse.wst.jsdt.ui.superType.container
    │      org.eclipse.wst.jsdt.ui.superType.name
    │
    ├─src
    │  └─main
    │      ├─java
    │      │  └─com
    │      │      └─kit
    │      │          ├─controller
    │      │          ├─filter
    │      │          │      FormFilter.java
    │      │          │
    │      │          ├─model
    │      │          └─util
    │      │                  DigitCaptcha.java
    │      │                  RequestFormStage.java
    │      │
    │      ├─resources
    │      └─webapp
    │          │  helloWorld.xhtml
    │          │  index.xhtml
    │          │  template.xhtml
    │          │
    │          ├─pathA
    │          │      a.xhtml
    │          │
    │          ├─pathB
    │          │      b.xhtml
    │          │
    │          └─WEB-INF
    │              │  .faces-config.xml.jsfdia
    │              │  beans.xml
    │              │  faces-config.xml
    │              │  web.xml
    │              │
    │              └─lib
    └─target
        ├─classes
        │  └─com
        │      └─kit
        │          ├─filter
        │          │      FormFilter.class
        │          │
        │          └─util
        │                  DigitCaptcha.class
        │                  RequestFormStage.class
        │
        ├─generated-sources
        │  └─annotations
        ├─maven-archiver
        │      pom.properties
        │
        └─webApp
            │  helloWorld.xhtml
            │  index.xhtml
            │  template.xhtml
            │
            ├─META-INF
            ├─pathA
            │      a.xhtml
            │
            ├─pathB
            │      b.xhtml
            │
            └─WEB-INF
                │  .faces-config.xml.jsfdia
                │  beans.xml
                │  faces-config.xml
                │  web.xml
                │
                ├─classes
                │  └─com
                │      └─kit
                │          ├─filter
                │          │      FormFilter.class
                │          │
                │          └─util
                │                  DigitCaptcha.class
                │                  RequestFormStage.class
                │
                └─lib
                        jboss-logging-3.1.3.GA.jar
                        primefaces-4.0.jar
    

1 个答案:

答案 0 :(得分:2)

/ pathB / *行引起了问题,删除行后,过滤器的重定向工作正常

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