<a href=""> link navigation doesn&#39;t detect templates in JSF 2.0 Facelets pages</a>

时间:2011-12-13 05:30:26

标签: jsf-2 facelets

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets">

<h:body>
    <title><ui:define name="title">Page 2</ui:define></title>
    <ui:composition template="template/common/commonLayout.xhtml">
        <ui:define name="content">
            This is the content of Page 2 page<br/>
            <h:form prependId="false">
                <h:commandButton id="goToIndex" value="Go to Index" action="index" />
            </h:form>
            <a href="index.xhtml">Index</a>
        </ui:define>

    </ui:composition>

</h:body>
</html>

我有两个相同的facelets页面:index.xhtml和page2.xhtml,它们相互链接。我还有一个模板文件。 index和page2之间的代码没有区别,除了标题中的页面名称,内容文本和commandButton值。

当我点击表单实现的goToIndex按钮导航到index.xhtml时,一切都按预期工作:它转到index.xhtml。但是,当我点击[a href]链接实现的链接导航到index.xhtml时,它会转到index.xhtml,但似乎忽略了页面的所有模板设置,包括任何表单标记。呈现的唯一内容是限制在“内容”定义中的文本,但没有任何css格式。

这件事情都发生了。 “index - &gt; page2”和“pag2 - &gt; index”

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:4)

<a href ...>调用的页面未由faces servlet处理,因此未正确翻译且未包含css / js。

查看 web.xml 并检查面部servlet的映射方式。在那里你可能会找到类似的东西:

<servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

您可以将模式更改为:

<url-pattern>*.xhtml</url-pattern>

然后所有带有xhtml前缀的文件将由faces servlet处理。但是,如果在项目中将xhtml前缀用于facelets之外的其他目的,这可能会导致问题。

另一种方法是使用h:link代替a:href

<h:link value="Index" outcome="index" >

其中outcome属性采用不包含.xhtml的目标页面。

相关问题