如何在JSF中使用jQuery

How to use jQuery in JSF?

本文关键字:jQuery JSF      更新时间:2023-09-26

嗨,我是JSF和jQuery的新手。我需要下载任何jQuery jar并打包到战争才能使用吗?因为在我的代码中我无法调用 jquery 函数。

<!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">
  <h:head>
   <script type="text/javascript" src="js/jquery-1.7.1.js"></script>
   <script type="text/javascript" src="js/06.js"></script>
   <script type="text/javascript">
  $(document).ready(function() {
      $("#form'':name").click(function() {
         alert("hii");
      });
   });
 </script>
 </h:head>
 <h:body>
    <h:form id="form">
      <h:outputText id="hi" value="hi guys"></h:outputText>
      <input id="name" />
    </h:form>
 </h:body>
</html>

当我单击输出框时,此警报不会弹出。

在我的pom中,我使用了这种依赖关系。

<dependency>
   <groupId>com.efsavage.jquery</groupId>
   <artifactId>jquery-maven</artifactId>
   <version>1.7.2</version>
</dependency>

您可以像在其他 Web 应用程序中一样使用它,jsf 标签也支持 onclick 属性。请下载我的JSF前端完全配置的Web应用程序(JSF2.2 + Primefaces+JQuery)并对其进行测试。这是一个链接,JSF2.2 前端

需要注意的是:Primefaces也使用了Jquery,你可以使用它,或者你可以跳过并包含你的jquery。在我的项目中,您可以找到配置的头文件。

这里也很好的例子 如何通过jQuery获取JSF id祝你好运!