【问题标题】:Mixing JSP and XHTML (Facelets) in JSF2 Project - possible?在 JSF2 项目中混合 JSP 和 XHTML (Facelets) - 可能吗?
【发布时间】:2011-04-25 13:36:09
【问题描述】:

我有一个客户想要使用 JSF2,他们喜欢 XHTML 现在是默认设置(Facelets)。

但是,他们的 JSF1.x 代码库中有大量“遗留”JSP。

我知道这可能是不可取的,但是否有可能在 JSF2 中支持两者的混合,至少在移植期间的过渡期内?

我知道可以在 JSF1.x 中混合使用这两者,但我在 JSF2 中找不到任何相关信息。

我用谷歌搜索过,但自然而然所有 JSF2 的焦点都集中在 Facelets 上。此外,我在混音方面的短暂尝试(我不是 JSF 专家!)也导致了失败。

【问题讨论】:

    标签: jsp jsf jsf-2 facelets


    【解决方案1】:

    这在Facelets FAQ 中得到了回答:在FacesServlet 上使用前缀映射。然后您可以通过http://example.com/faces/page.jsp 访问JSP 页面,通过http://example.com/faces/page.xhtml 访问Facelets 页面。这是相关性的引用:

    How do I use Facelets and JSP in the same application?

    您必须对 Facelets 页面使用前缀映射才能使其正常工作。将 DEFAULT_SUFFIX 保留为 JSF 默认值 .jsp。配置 Facelet 的VIEW_MAPPINGS 参数:

    <web-app>
        <context-param>
            <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
            <param-value>.jsp</param-value>
        </context-param>
    
        <!-- Facelets pages will use the .xhtml extension -->
        <context-param>
            <param-name>facelets.VIEW_MAPPINGS</param-name>
            <param-value>*.xhtml</param-value>
        </context-param>     
    
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        </servlet>
    
        <!-- Use prefix mapping for Facelets pages, e.g. http://localhost:8080/webapp/faces/mypage.xhtml -->
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

    【讨论】:

    • 啊,谢谢。我看过那个页面,但因为我认为它是 JSF1.x 并且已经过时而打折了它。我曾尝试过与上面的 web.xml 类似的策略,但失败了——这是我的愚蠢错误。将重试谢谢!
    • 仅供参考:另一种方法是使用*.jsf 的后缀映射。 JSF2 将首先扫描 XHTML 文件。如果不存在,那么它将扫描 JSP 文件。另见stackoverflow.com/questions/4441713/…
    【解决方案2】:

    BalusC 引用的 wiki 部分似乎确实已经过时了。在我的扩展映射 (*.faces) 设置中,我遇到了将建议的 javax.faces.DEFAULT_SUFFIX 设置为 .jsp 的问题,该问题在 *.xhtml 页面的表单标签内生成的操作 URL 具有 .jsp 扩展名而不是 .faces 扩展名(并且因此无法映射)。

    在我步入 Apache MyFaces 2.x 实现的相应类之后(参见 org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateActionURL(FacesContext context, String viewId))如下事实证明,设置在我们并行使用 JSP 和 Facelets 视图处理时起作用。

    如何在同一个应用程序中使用 Facelets 和 JSP?

    除了前缀映射之外,您还可以对 Facelets 页面使用扩展映射(例如 *.faces)以使其正常工作。将 DEFAULT_SUFFIX 保留为 JSF 默认值 .jsp .xhtml。配置 Facelet 的 VIEW_MAPPINGS 参数:

    <web-app>
        <context-param>
            <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
            <param-value>.jsp .xhtml</param-value>
        </context-param>
    
        <!-- Facelets pages will use the .xhtml extension -->
        <context-param>
            <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
            <param-value>*.xhtml</param-value>
        </context-param>     
    
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        </servlet>
    
        <!-- use extension mapping in this sample -->
        <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>*.faces</url-pattern>
        </servlet-mapping>
    </web-app>
    

    对于那些对 org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateActionURL(FacesContext context, String viewId) 中的 action url 处理细节感兴趣的人:

            if ( mapping.isExtensionMapping() ) {
                // See JSF 2.0 section 7.5.2
                String[] contextSuffixes = _initialized ? _contextSuffixes : getContextSuffix( context );
                boolean founded = false;
                for ( String contextSuffix : contextSuffixes ) {
                    if ( viewId.endsWith( contextSuffix ) ) {
                        builder.append( viewId.substring( 0, viewId.indexOf( contextSuffix ) ) );
                        builder.append( mapping.getExtension() );
                        founded = true;
                        break;
                    }
                }
                if ( !founded ) {
                    // See JSF 2.0 section 7.5.2
                    // - If the argument viewId has an extension, and this extension is mapping,
                    // the result is contextPath + viewId
                    //
                    // -= Leonardo Uribe =- It is evident that when the page is generated, the
                    // derived
                    // viewId will end with the
                    // right contextSuffix, and a navigation entry on faces-config.xml should use
                    // such id,
                    // this is just a workaroud
                    // for usability. There is a potential risk that change the mapping in a webapp
                    // make
                    // the same application fail,
                    // so use viewIds ending with mapping extensions is not a good practice.
                    if ( viewId.endsWith( mapping.getExtension() ) ) {
                        builder.append( viewId );
                    } else if ( viewId.lastIndexOf( "." ) != -1 ) {
                        builder.append( viewId.substring( 0, viewId.lastIndexOf( "." ) ) );
                        builder.append( contextSuffixes[0] );
                    } else {
                        builder.append( viewId );
                        builder.append( contextSuffixes[0] );
                    }
                }
            } else {
                builder.append( mapping.getPrefix() );
                builder.append( viewId );
            }
    

    【讨论】:

      【解决方案3】:

      上面的建议对我根本不起作用。 wiki 页面可能已过时。从 JSF2 规范中,我得到了以下有效的参数:

        <!-- Facelets pages will use the .xhtml extension -->
        <context-param>
          <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
          <param-value>*.xhtml</param-value>
        </context-param> 
      

      代替:

      <context-param>
          <param-name>facelets.VIEW_MAPPINGS</param-name>
          <param-value>*.xhtml</param-value>
      </context-param>
      

      【讨论】:

      • 但是,对 .jsf 映射进行更改确实有效,但更改此设置需要很长时间才能修复,但如果您不介意更改 url,则效果很好。
      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 2013-10-02
      • 2016-11-24
      相关资源
      最近更新 更多