Javascript 在 JSF 模板下不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11027285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript does not work under JSF template
提问by hajime
I'm using JSF templates and Primefaces.
我正在使用 JSF 模板和 Primefaces。
Javascript code does not seem to be working under ui:composition and ui:define tags. The following code is not hitting the loaded() method. This is the content.xhtml file
Javascript 代码在 ui:composition 和 ui:define 标签下似乎不起作用。以下代码未命中loaded() 方法。这是 content.xhtml 文件
<h:head>
<script language="javascript">
function loaded() {
alert("Working!!");
}
</script>
</h:head>
<ui:composition template="/template/template.xhtml">
<ui:define name="content">
<h:body style="width:100%;height:100%;" onload="loaded()">
<p class="item">Random text</p>
</h:body>
</ui:define>
</ui:composition>
but when i remove the define and composition tags the loaded function is called. Any idea why this is happening ?
但是当我删除定义和组合标签时,会调用加载的函数。知道为什么会这样吗?
Here is the template file
这是模板文件
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title><ui:insert name="title">Template</ui:insert></title>
</h:head>
<h:body>
<div id="header">
<ui:insert name="header">
<ui:include src="../menu.xhtml" />
</ui:insert>
</div>
<div id="content">
<ui:insert name="content">
<ui:include src="../content.xhtml" />
</ui:insert>
</div>
<div id="footer">
<ui:insert name="footer">
This is a footer
</ui:insert>
</div>
回答by BalusC
Everything outside<ui:composition>
is ignoredduring building the view. Also, redeclaring <h:body>
once again is unnecessary. To use a script which runs during on page load, better use a <h:outputScript target="body">
. This will be relocated to end of body and thus be invoked afterthe necessary HTML DOM elements are been built. This is also somewhat faster than an onload
.
在构建视图期间,外部的所有内容都将<ui:composition>
被忽略。此外,<h:body>
再次重新声明是不必要的。要使用在页面加载期间运行的脚本,最好使用<h:outputScript target="body">
. 这将被重新定位到正文的末尾,因此在构建必要的 HTML DOM 元素后被调用。这也比onload
.
All with all, your entirecontent.xhtml
must look like this:
总之,你的整个人content.xhtml
必须是这样的:
<ui:composition template="/template/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<ui:define name="content">
<h:outputScript target="body">
alert("Working!!");
</h:outputScript>
<p class="item">Random text</p>
</ui:define>
</ui:composition>
See also:
也可以看看:
回答by Steve H.
You are passing "content" to the template. If your template does not include "content", it won't be including in the resulting HTML.
您正在将“内容”传递给模板。如果您的模板不包含“内容”,则它不会包含在生成的 HTML 中。
Post the template.xhtml and let's see...
发布template.xhtml,让我们看看...