Getting an EL in a Javascript file, loaded by @ResourceDependency
Asked Answered
J

1

8

I'm using @ResourceDependency annotation in a JSF component to add Javascript and CSS files into my JSF page.

In my Javascript file, I need to reference another resource file (a .swf file, which located in META-INF/resources, as JSF requires). I tried to put a #{resource['swf:file.swf']} EL expression in my javascript code, but it won't get resolved.

For exapmle, for the following JS file in the server:

var instance = new JSClass();
instance.setResourceUrl("#{resource['swf:file.swf']}");

The browser gets:

var instance = new JSClass();
instance.setResourceUrl("#{resource['swf:file.swf']}");

which is wrong.

While when I put the same EL in the CSS file, it get resolved properly. For the following CSS file in the server:

.instance-css-class {
  background: url("#{resource['swf:file.swf']}")
}

The browser gets:

.instance-css-class {
  background: url("/webapp/javax.faces.resource/file.swf.jsf?ln=swf")
}

Which is exactly what I need, but in the JS file.

Obviously, I can use the CSS as a workaround for the issue (Create a DOM element, attach the CSS class to it, and then read and parse the required style property). But, is there a more elegant way to achive it? Is it a bug in the JSF library (I'm using Mojarra 2.0.3, with Jboss 6.1), or there is a reason for that behavior?

Please mind that the code above is part of a tag library, so workarounds such as those can't be used.


Edit - Seems that the CSS workaround is not feasible, since I can't see a way to get CSS attribute from a CSS file. So any (working) workaround would be gladly accepted as well.

Jacquelynnjacquenetta answered 28/8, 2011 at 9:45 Comment(1)
I just recently found out, to my surprise, that EL is evaluated in CSS. The Mojarra implementation uses a ResourceHelper that is capable of resolving EL Expressions within resources requested by a Faces Resource Request. Thats why I wonder that EL is not evaluated in JS files as it is the same type of request. Would be great if anyone can shed some light on this.Silicium
B
0

BalusC has answered a similar question in detail here. Though the first solution is the easiest to implement, I would recommend you to go with the third, which in my opinion, is the correct way.

Bibbye answered 4/5, 2012 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.