File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Expression Language inside javascript file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Expression Language inside javascript file" Watch "Expression Language inside javascript file" New topic

Expression Language inside javascript file

Raphael Guimenes

Joined: Mar 13, 2012
Posts: 6
Hello All!!

I need to use Expression Language inside external javascript file. I'm using JSF 2.0.

I need to use this because I have to use messages I18n at javascript file.

I tried to use the follow, but not works:

However the follow it works inside external css file:

I included both (file JS and CSS) using Primefaces:

can someone help-me?


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17280

Welcome to the JavaRanch, Raphael!

I have doubts that your EL in your CSS file really works. At least if it's a real CSS file retrieved by a real URL.

Neither CSS nor JavaScript files should be processing EL for the simple reason that they don't pass through an EL processor. In modern-day JEE, there's a Unified EL subsystem and it will deal with EL in JSF View Definition Language and with plain JSPs, but it isn't applied to non-JEE resources. In part because running EL on something like a binary image file would be a disaster!

Your CSS also has another issue. The FacesContext is not a permanent or long-term object. It exists only for the duration of the request process for the URL. If that URL wasn't routed to the FacesServlet, the FacesContext won't exist for that request.

So the only way you could get EL to work in either js or css files would be to brute-force include them as part of the source of a JSF page. If they're fetched as distinct URLs (such as via the <LINK> element, they won't go through the EL processor.

Last, but not least, there's the addressability issue. JavaScript runs on the client. Your properties live on the server. So it takes more than just EL if you want general access to them.

The outputScript and outputStyleSheet elements are new to JSF2 and the documentation I've found for them is pretty vile and looks to be more for the benefit of the tag implementor than it is for people who are actually using the tag. But as far as I'm aware, these elements do NOT route their respective resources through the FacesServlet, they just generate JSF-friendly HTML. Primarily that means that the output URLs know about the servlet context path without making you supply it explicitly.

An IDE is no substitute for an Intelligent Developer.
Raphael Guimenes

Joined: Mar 13, 2012
Posts: 6
Hello tim, thank you for your help.

But i'm doubt about your comments on putputScript and putputStylesheet.

I analysed the page rendered (html created by server) and i saw the follow:

Tags PrimeFaces:

Tabs Primefaces after rendered in the html:

Note that after rendered was created a link for the JSF resources. And same link was created for archive with extension xhtml and not with js

Because of this i belive that EL works inside this JavaScript and CSS (this i tested and it works).

What do you think?

Thank you again.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17280

Those aren't PrimeFaces tags. Tags in the "h" namespace come from the Core JSF2 tagset.

But you're right. If it went to all the trouble of constructing an "xhtml" URL, it should be passing through something.

The one thing that doesn't ring quite true, however, is that an ".xhtml" URL doesn't get routed to the FacesServlet, normally. Instead, you'd request a ".jsf" URL and the FacesServlet would break it down, locate the .xhtml resource that matched the basename, and process that. So I'm not quite sure what's going on here. I need to try these critters myself. I missed hearing about them, and have been using the RichFaces equivalents, instead. Until JSF2 came out, that was the only way to do this kind of stuff, so that's what I've been coding.

Assuming that the JavaScript is going through the EL processor, the one caveat there is that the expansion will be done by EL, not dynamically. I doubt that it matters in your case, though.
I agree. Here's the link:
subject: Expression Language inside javascript file
It's not a secret anymore!