JSF模板:缺少布局

  • 本文关键字:布局 模板 JSF jsf
  • 更新时间 :
  • 英文 :


我是JSF的新手,使用JSF模板有一些问题。模板newTemplate.xhtml)的代码如下所示:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
        <link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
        <title><ui:insert name="title">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <div id="top">
            <ui:insert name="top">Top</ui:insert>
        </div>
        <div>
            <div id="left">
                <ui:insert name="left">Left</ui:insert>
                <h:form>
                    <h:commandButton id="test" value="Test" action="/jsf/newTemplateClient.xhtml"/>
                </h:form>
            </div>
            <div id="content" class="left_content">
                <ui:insert name="content">Content</ui:insert>
            </div>
        </div>
        <div id="bottom">
            <ui:insert name="bottom">Bottom</ui:insert>
        </div>
    </h:body>
</html>    

名为index1.xhtml的起始页如下所示:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets">
    <body>
        <ui:composition template="/WEB-INF/newTemplate.xhtml">
            <ui:define name="title">
                TEST
            </ui:define>
        </ui:composition>
    </body>
</html>

最终模板客户端页面(称为newTemplateClient.xhtml):

<ui:composition template="/WEB-INF/newTemplate.xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets">
   <ui:define name="content">
        Some content in newTemplateClient.xhtml
   </ui:define>
</ui:composition>

下面是应用程序的行为:

  1. 应用程序启动(地址栏显示localhost:8080/WebTEST/),布局和内容显示正常,
  2. 我点击"测试"按钮,newTemplateClient.xhtml的内容也被正确显示(地址更改为:localhost:8080/WebTEST/faces/index1.xhtml),
  3. 我再次点击"测试"按钮,显示内容,但我失去了布局,就像没有应用css样式(没有颜色和格式)。现在地址栏显示:localhost:8080/WebTEST/faces/jsf/newTemplateClient.xhtml

模板文件位于WEB-INF文件夹,newTemplateClient.xhtml位于jsf文件夹。

我知道这可能是一些简单的事情,但我真的不知道是什么原因导致这个问题

重复我的评论,问题是CSS链接中的相对URL在离开索引页后会解析为不同的绝对URL。但是,您可以使用一个不错的JSF标记来代替原始链接来解决您的问题:使用<h:outputStylesheet library="css" value="default.css">代替<link href=...>

最新更新