How to define an onLoad function in JSF template that will be defined elsewhere

流过昼夜 提交于 2019-12-21 17:49:03

问题


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:

<div id="content"> <ui:insert name="content"/> </div>

Then in login.xhtml I have:

<ui:define name="content"> ... </ui:define>

Normally I would put this in login.xhtml:

<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).

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?

Thanks!


回答1:


I can think of at least two ways:

  1. 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();">

  2. use facelets params. I.e. <body onload="#{onLoadJS}"/> and on each page including the template use <ui:param name="onLoadJS" value="document.getElementById(..)" />



来源:https://stackoverflow.com/questions/2375676/how-to-define-an-onload-function-in-jsf-template-that-will-be-defined-elsewhere

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!