簡體   English   中英

在JSF2項目中混合使用JSP和XHTML(Facelets) - 可能嗎?

[英]Mixing JSP and XHTML (Facelets) in JSF2 Project - possible?

我有一個客戶想要使用JSF2,他們喜歡XHTML現在是默認的(Facelets)。

但是,他們的JSF1.x代碼庫中有大量的“遺留”JSP。

我知道這可能不太可取,但是有可能在JSF2中支持兩者的混合,至少在他們移植的過渡期間嗎?

我知道可以在JSF1.x中混合使用兩者,但我在JSF2中找不到任何關於此的信息。

我用谷歌搜索過,但自然所有JSF2都集中在Facelets上。 我對混音的簡短嘗試(我不是JSF的專家!)導致了失敗。

這在Facelets FAQ中得到了解答 :在FacesServlet上使用前綴映射。 然后,您可以通過http://example.com/faces/page.jsp和Facelets頁面訪問JSP頁面,網址為http://example.com/faces/page.xhtml 這是一個相關的引用:

如何在同一個應用程序中使用Facelets和JSP?

您必須為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, eg http://localhost:8080/webapp/faces/mypage.xhtml --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> </web-app> 

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)中的動作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 );
        }

上述建議對我來說根本不起作用。 維基頁面可能已過期。 從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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM