jQuery not running in JSF Application

北城余情 提交于 2019-12-24 10:45:06

问题


I am newbie to jQuery.

In netbeans I have jquery-1.7.2.js where all HTML files are present. I am creating JSF 2.0 project.

Below is the code I have

<?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:f="http://java.sun.com/jsf/core"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:head>
        <title>Welcome to MySite.</title>
    </h:head>

    <script language="text/javascript" src="#{facesContext.externalContext.requestContextPath}/faces/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            alert('I am here...');
            $("#table tr:last").after("<tr><td>some</td><td>content</td></tr>");
        });

    </script>

I believe with this code, whenever document gets loaded, I should have got alert as Hi, I am here... however I am not getting any alert.

Also, when I see view source & click on jquery-1.7.2.js, it gets opened.

Any idea why I am not getting alert?

Update 1

I am following tutorial provided here, still no luck. :(


回答1:


It seems like a bad location of file, I dont where exactly you placed the jquery file in your web app , but anyway It should be located in folder under the resources folder, So...

Add resources folder under the WebContent

and inside resources create js folder

then access the files like this

 <h:outputScript name="js/jquery-1.7.2.js" target="head" />



回答2:


I believe that JSF may be enforcing XHTML standards which tell it to ignore your script tag because it is outisde head or body tags. Have you checked whether your script tag is being rendered at all?

I have learned the hard way that the best way to do stuff when dealing with JSF is to do it the JSF way. In this case, it would be using h:outputScript tag.

Also, a good practice with JavaScript is to put it in a separate .js file even if you are going to use it on only one page. Makes it much easier to debug in a browser.




回答3:


Are you sure about the line: "#{facesContext.externalContext.requestContextPath}/faces/jquery-1.7.2.js" ? If I were you, I would look at the browser Url, then code with a relative path w/r to that Url to point to jquery-1.7.2.js. Another possible debug way would be to make a copy of the jquery-1.7.2.js in the current folder (i.e. of the current .xhtml), then the script tag line would be src="./jquery-1.7.2.js". If you can see the alert, then the problem is from that line path.



来源:https://stackoverflow.com/questions/11333409/jquery-not-running-in-jsf-application

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