我是jsf新手。我一直在尝试用 commandbutton 做一个简单的 Javascript 函数。我尝试了很多次,但甚至无法发出警报消息。这是我的代码的一部分。请任何人指导我,并告诉我出了什么问题,以及我应该做什么才能让它运行?
<?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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<script type="text/javascript">
function test(){
alert('test');
alert(document.getElementById('frmDashBoard:testbt').value);
}
</script>
</h:head>
<h:body>
<ui:composition template="../../template/commonLayout.xhtml">
<ui:define name="content">
<div>
<h:form id="frmdashboard">
<div name="form_panel" style="width: 984px">
<h:commandButton id="testbt" value="#{message.btn_dashboard_search}" action="#{searchBLAction.doAction}" onclick="test()" />
</div>
</h:form>
</div>
</ui:define>
</ui:composition>
</h:body>
</html>
除了这个小写/大写拼写错误(顺便说一句,这不会导致函数根本没有被调用)之外,您的具体问题是因为该页面被设计为模板客户端而导致的<ui:composition>
。任何超出范围的内容<ui:composition>
tag is ignored在 Facelets 运行时。此内容仅对可视网页设计者有用,但一旦在运行时编译并执行,它就会被完全忽略。相反,合成的内容将被插入到主模板中,commonLayout
在你的情况下。
你需要把<script>
里面的元素<ui:define>
相反,这样它将被纳入最终的 Facelets 组合中。
<ui:define name="...">
<script>
...
</script>
...
</ui:define>
或者,更好的方法是,将 JS 函数放在其自己的 JS 文件中/resources
文件夹并按如下方式引用它:
<ui:define name="...">
<h:outputScript name="some.js" target="head" />
...
</ui:define>
感谢target="head"
,它会自动重新定位到 HTML 中<head>
在构建视图期间。
也可以看看:
- 如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML? https://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets/4793959#4793959
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)