File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes JS comparison in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JS comparison in JSF" Watch "JS comparison in JSF" New topic

JS comparison in JSF

Celia Xu

Joined: May 02, 2012
Posts: 1
Hi guys,

I have a jsp page and in my <f:view> tag I want to put my JS :
for example:

i tried to put it either between<head></head> or <body></body> (also tried put <f:verbatim> around)
BUT when i inspect element i found when I use "<", all the lines after that in js messed up:

because of this, it will throw errors if i want to use this function.
However, it will be fine if i use any other comparison operators.
anyone has idea?
Nikhil Padharia
Ranch Hand

Joined: Apr 19, 2012
Posts: 37
use Ampersand - & lt; in place of < for comparision.
All four letters should be clubbed together.. forum is automatically converting it to < if i post such..
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Welcome to the JavaRanch, Celia!

The JSF View Definition Language (VDL) is XML and in XML, there are 5 "magical" characters: & ' " < &rt;. Because these characters are part of the XML "language" itself, it's possible to confuse the XML parser when you use them for non-XML purposes, such as inside JavaScript functions.

There are 3 ways to avoid this problem

1. Use XML entities, like Nikhil recommended. I think he ran into problems with our XML parser, so he had to mis-type slightly. The entity code for the ampersand character is actually "&" and please note that the final ";" character is required.

2. Use XML CDATA escapes. Like so:

Note the "//" comment marker before the CDATA close "]>". You're dealing with about 3 different syntax forms at the same time so things like this avoid having the wrong thing parsed at the wrong time.

3. Finally, the easiest solution of them all. Put the JavaScript in a separate Javascript-only resource file and include it using the HTML LINK directive or something similar. Since the js file is a completely different file being accessed from the client, it won't be parsed by the JSF view digester.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: JS comparison in JSF
It's not a secret anymore!