【问题标题】:<h:outputScript> target problem when using templates<h:outputScript> 使用模板时的目标问题
【发布时间】:2011-05-10 18:55:59
【问题描述】:

我有一个关于将 jquery 库与 JSF 2.0 集成的问题

当使用 &lt;h:outputScript library="/common/js" name="jquery-1.5.1.min.js" target="head" /&gt; 时,我应该在我的 xhtml 文件中也包含 &lt;h:head&gt; 标记。所以脚本在头部呈现。

但我有一个包含 &lt;h:head&gt;,&lt;h:body&gt; 部分的 template.xhtml。我如何为从该模板派生的页面创建target="head" /&gt; ui:composition="template.xhtml"

target=form&gt; 也不起作用。

我的模板:

<?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:p="http://primefaces.prime.com.tr/ui"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><h:outputText value="#{msg['label.titlemsg']}" /></title>
    <h:outputStylesheet library="css" name="style.css"  target="head"/>
</h:head>

<h:body>
<f:view locale="#{localeBean.locale}">

<div id="outer">

  <div id="container"> 
    <div id="inner"> 

      <div class="float">
        <div class="main"> 
          <!-- -->
          <div id="icerik"> 

              <ui:insert name="icerik">



      </ui:insert>
    </div></div></div></div></div></div>

     <div id="langbar" align="center">


    </div>

</f:view>
</h:body>
</html>

所以我有“icerik”。

在我的文件中:

<!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:p="http://primefaces.prime.com.tr/ui"  
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/Template.xhtml">

    <ui:define name="icerik">

    <h:outputScript library="/common/js" name="jquery-1.5.1.min.js"  target="head" />
<h:outputScript library="/common/js" name="jquery.validate.js" target="head" /> 


    </ui:define>

</ui:composition>
</html>

【问题讨论】:

  • 它应该这样工作。相反会发生什么?当您在 webbrowser 中右键单击页面并查看 JSF 生成的 HTML 输出时,您会看到什么?想要的&lt;script&gt; 在那里吗?
  • 嗨,我使用模板,所以 target=head 和 target=body 不会生成关于 .js 文件的输出。
  • 嗨,我用一些代码更新了我的问题。
  • 您能更清楚地了解会发生什么吗?您在生成的 HTML 输出的头部看到 nothing 了吗?甚至没有&lt;script type="text/javascript" src="RES_NOT_FOUND"&gt;
  • 是的,不使用模板我的 .js 文件可以在 head 标签中看到。使用模板对我来说会导致这个问题。如果使用 ui:define,则头部不显示任何内容

标签: jsf facelets


【解决方案1】:

我认为更好的方法解决了我的问题:将&lt;ui:insert name="icerikhead"&gt;&lt;/ui:insert&gt; 添加到模板的头部,然后将其填写为&lt;ui:define name="icerikhead"&gt; add js files &lt;/ui:define&gt;

【讨论】:

  • 4 年后,我遇到了同样的问题。在 facelets ui:composition 中使用 h:outputScript 时,不会呈现脚本标签。此 facelet 还使用模板并通过一些 ui:define 标记定义内容。然后我意识到,我必须将 h:outputScript 放在这些 ui:define 标记之一中,而不是在它们之外。现在它就像一个魅力。
猜你喜欢
  • 2013-07-19
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多