我有一个想要使用JSF2的客户,他们喜欢XHTML现在是默认的(Facelets)。
但是,他们的JSF1.x代码库中有大量的“遗留”JSP。
我知道这可能不太可取,但是有可能在JSF2中支持两者的混合,至少在他们移植的过渡期间吗?
我知道可以在JSF1.x中混合使用两者,但我在JSF2中找不到任何关于此的信息。
我用Google搜索过,但自然所有JSF2都集中在Facelets上。此外,我对混音的简短尝试(我不是JSF的专家!)导致了失败。
答案 0 :(得分:12)
在FacesServlet
上的Facelets FAQ:使用前缀映射中对此进行了解答。然后,您可以按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>
答案 1 :(得分:3)
BalusC引用的wiki部分似乎确实已经过时了。在我的扩展映射(* .faces)设置中,我遇到问题,建议javax.faces.DEFAULT_SUFFIX
设置为.jsp
,在* .xhtml页面的表单标记内生成操作URL,而不是a。扩展名.jsp。面部扩展(因此无法映射)。
在我进入Apache MyFaces 2.x实现的相应类之后(参见 org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateActionURL(FacesContext context,String viewId))以下内容安装结果在我们并行使用JSP和Facelets视图处理方面起作用。
除了前缀映射之外,您还可以使用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)中的动作网址处理细节感兴趣的人:
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 );
}
答案 2 :(得分:1)
上述建议对我来说根本不起作用。维基页面可能已过期。从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>