3

I am new to Javascript and Java Server Faces and I am having the next Problem.

The Code below is working ok:

<?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:ui="http://java.sun.com/jsf/facelets">
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript">
        function initialize() 
        {
            alert("Testing");
        }
    </script>
    </head>
    <body onload="initialize()">

        <h1 align="center">Sol-Tech</h1><br  />

    </body>
</html>

But when I add a FOR loop, it doesn't work:

<?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:ui="http://java.sun.com/jsf/facelets">
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript">
        function initialize() 
        {
            for(var i; i<3; i++)
            {
                alert("Test");  
            }
        }
    </script>
    </head>
    <body onload="initialize()">

        <h1 align="center">Sol-Tech</h1><br  />

    </body>
</html>

enter image description here

Has anyone any suggestion on how to perform a FOR loop in javascript without getting an error?

Thanks in advance, Emanuel

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mick Morrison
  • 105
  • 1
  • 5

1 Answers1

4

You're using an outdated version of Mojarra which exposes a bug wherein this kind of IllegalArgumentException: null source is incorrectly been thrown when the view file contains a XML syntax error or when the view couldn't be restored. See also java.lang.IllegalArgumentException: null source and JSF issue 1762.

If you upgrade to a newer Mojarra version (currently already 2.1.14), then you'll in this particular case get a more self-explaining XML syntax error on the character < which indicates the start of a XML element. Facelets is namely a XML based view technology and parsed by a SAX parser. You'd need to replace the XML special character < by &lt;

for(var i=0; i&lt;3; i++) {
    alert("Test");  
}

or to put the whole script in a CDATA block

<script type="text/javascript">
  <![CDATA[
    function initialize() 
    {
        for(var i=0; i<3; i++)
        {
            alert("Test");  
        }
    }
  ]]>
</script>

or to just put it in its own .js file so that it won't be parsed as XML.

<script type="text/javascript" src="script.js"></script>

See also:


Unrelated to the concrete problem, the JS syntax error (uninitialized var i which I've already fixed in code snippets) is actually a completely different problem and would only show an error in client side in browser's JS console and definitely not cause an exception in the server side as JS doesn't run in webserver at all, but only in webbrowser.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555