首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JSF2项目中混合使用JSP和XHTML (Facelets) -可能吗?

在JSF2项目中混合使用JSP和XHTML (Facelets) -可能吗?
EN

Stack Overflow用户
提问于 2010-10-15 02:00:45
回答 3查看 22.3K关注 0票数 9

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

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

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

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

我用谷歌搜索了很久,但自然所有的JSF2焦点都集中在Facelets上。还有我对混合的简短尝试(我不是JSF的专家!)已经导致了失败。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-10-15 05:56:41

这在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参数:

javax.faces.DEFAULT_SUFFIX .jsp facelets.VIEW_MAPPINGS *.xhtml Faces Servlet javax.faces.webapp.FacesServlet Faces Servlet /faces/*

票数 13
EN

Stack Overflow用户

发布于 2013-04-12 19:30:07

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

在我步入ApacheJSP2.x实现的相应类(请参阅org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateActionURL(FacesContext上下文,字符串viewId)之后,以下设置在并行使用MyFaces和Facelets View处理时可以正常工作。

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

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

代码语言:javascript
复制
<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中操作urls的处理细节感兴趣的人,请使用字符串viewId):

代码语言:javascript
复制
        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
EN

Stack Overflow用户

发布于 2011-11-30 20:28:21

上面的建议对我一点都不起作用。维基页面可能已经过时了。从JSF2规范中,我获得了以下工作参数:

代码语言:javascript
复制
  <!-- 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> 

而不是:

代码语言:javascript
复制
<context-param>
    <param-name>facelets.VIEW_MAPPINGS</param-name>
    <param-value>*.xhtml</param-value>
</context-param>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3936110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档