java 如何在 JSF 模板中定义一个将在别处定义的 onLoad 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2375676/
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
How to define an onLoad function in JSF template that will be defined elsewhere
提问by purecharger
I'm designing the view for my site, which has a standard login and landing page, and I want to have an onLoad function called for my login page, but not for my other pages (yet). I've got a template.xhtml file, which has this insert:
我正在为我的网站设计视图,它有一个标准的登录和登录页面,我想为我的登录页面调用一个 onLoad 函数,但不是我的其他页面(还)。我有一个 template.xhtml 文件,其中包含以下内容:
<div id="content">
<ui:insert name="content"/>
</div>
<div id="content">
<ui:insert name="content"/>
</div>
Then in login.xhtml I have:
然后在 login.xhtml 我有:
<ui:define name="content">
...
</ui:define>
<ui:define name="content">
...
</ui:define>
Normally I would put this in login.xhtml:
通常我会把它放在 login.xhtml 中:
<body onload="document.getElementById('login_form:name').focus();">
<body onload="document.getElementById('login_form:name').focus();">
But since I'm using JSF's ui composition tags, I can't have the <body/>tag in login.xhtml (at least the way I am attempting to do it).
但是由于我使用的是 JSF 的 ui 组合标签,所以我不能<body/>在 login.xhtml 中使用该标签(至少我尝试这样做的方式是这样)。
Is there a way to accomplish this with the structure I've described? The way I would think of doing it is to have onLoad call a function in the template, and then each page with ui:define would populate this function. Is that possible?
有没有办法用我描述的结构来完成这个?我想这样做的方法是让 onLoad 调用模板中的一个函数,然后每个带有 ui:define 的页面都会填充这个函数。那可能吗?
Thanks!
谢谢!
回答by Bozho
I can think of at least two ways:
我至少可以想到两种方法:
define the header section with
<ui:define name="header">, and put a javascript function (function bodyLoaded(){..}) in it - different on every page, and then reference it via<body onload="bodyLoaded();">use facelets params. I.e.
<body onload="#{onLoadJS}"/>and on each page including the template use<ui:param name="onLoadJS" value="document.getElementById(..)" />
使用 定义标题部分
<ui:define name="header">,并在其中放置一个 javascript 函数 (function bodyLoaded(){..}) - 每个页面都不同,然后通过<body onload="bodyLoaded();">使用 facelets 参数。即
<body onload="#{onLoadJS}"/>和在每个页面上,包括模板使用<ui:param name="onLoadJS" value="document.getElementById(..)" />

