无法在jsf页面中执行Javascript [英] Can't execute Javascript in jsf page

查看:142
本文介绍了无法在jsf页面中执行Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jsf的新手。我一直试图用commandbutton做一个简单的Javascript函数。我尝试了很多次,但甚至无法发出警报信息。这是我的代码的一部分。请任何人都可以指导我,告诉我有什么问题,以及我应该怎样做才能让它运行?

I'm new to jsf. I have been trying to do a simple Javascript function with commandbutton. I tried many times but wasn't even able to do an alert message. This is part of my code. Please can anyone guide me, and tell what is wrong, and what I should do to make it run?

<?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> 。在Facelets运行时,< ui:composition> 标记之外的任何内容都忽略。此内容仅对可视化Web设计人员有用,但一旦在运行时编译和执行,它就会被完全忽略。相反,组合的内容将插入主模板中,在您的情况下 commonLayout

Apart from this lowercase/uppercase typo (which wouldn't cause the function not being called at all, by the way), your concrete problem is caused because this page is been designed as a template client using <ui:composition>. Any content outside the <ui:composition> tag is ignored during runtime by Facelets. This content is only useful for visual web designers, but once it's compiled and executed during runtime, it's ignored altogether. Instead the composition's content will be inserted in the master template, the commonLayout in your case.

你需要把< ui:define> 中的< script> 元素,这样它将被带入最终Facelets组合。

You need to put the <script> element inside an <ui:define> instead, this way it will be taken into the final Facelets composition.

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

或者,更好的是,将JS函数放在中的JS文件中/ resources 文件夹并按如下方式引用它:

Or, better, put the JS function in its own JS file in the /resources folder and reference it as follows:

<ui:define name="...">
    <h:outputScript name="some.js" target="head" />
    ...
</ui:define>

感谢 target =head ,在构建视图时,它会自动重新定位到HTML < head>

Thanks to the target="head", it'll automatically be relocated into the HTML <head> during building the view.

  • How to include another XHTML in XHTML using JSF 2.0 Facelets?

这篇关于无法在jsf页面中执行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆