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 );
}