1

I use some jquery functions in my jsf project and I want to get one image from resources folder of my project from this function :

I tested this :

$('.ui-datagrid-column').live('mousemove',function(){
            $(this).css('background-image', '../resources/images/vert_degrade.PNG');        

        });

but it doesn't find the image file

here is the structure : enter image description here

and this file is in the page directory

how can I achieve this

1 Answer 1

1

If you specify a relative URL in CSS background-image property, then it becomes relative to the request URL of the currently requested resource containing the JS code. So, if you declared it in a JS file, then it's relative to the request URL of the JS file itself. Or, if you declared it in a JSF page, then it's relative to the request URL of the JSF file itself. It's not relative to the physical location in the server side disk file system path as most starters incorrectly assume.

Thus, that relative URL is in your case clearly wrong. In a JSF web application, among others the JSF resource handler's URL prefix /javax.faces.resource or even the faces servlet mapping like /faces/* are most probable causes that the relative URL resolves wrongly. You need to look at the request URL and extract the proper relative URL from it.

Or, if that JS code is located inside a <script> or <h:outputScript> of a JSF page, then you can also use the implicit #{resource} mapping in EL to convert a resource identifier to a fullworthy resource URL:

E.g.

<h:outputScript>
    $('.ui-datagrid-column').live('mousemove',function(){
        $(this).css('background-image', '#{resource['images/vert_degrade.PNG']}');        
    });
</h:outputScript>

Much better is to create a CSS declaration in a standalone CSS file which is loaded by <h:outputStylesheet> as it supports evaluating EL expressions inside CSS files.

.vert-degrade {
    background-image: url('#{resource['images/vert_degrade.PNG']}');
}

with

$('.ui-datagrid-column').live('mousemove',function(){
    $(this).addClass('vert-degrade');
});

so that the JS code can be kept in its own JS file.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you BalusC, I use this code inside <script> but when I write your code, I have as error : com.sun.faces.context.ExternalContextImpl getMimeType ATTENTION: JSF1091 : Aucun type mime détecté pour le fichier images/vert_degrade.PNG. Pour résoudre ce problème, ajoutez un mappage mime-type au fichier web.xml de l’application.
Lowercase extension to png. The extension PNG is invalid. Hint: please change your environment locale to English. Error messages in English yield so much more helpful results in Google. I didn't bother to pass it through Google Translate or so, I just recognized this error by code "JSF1091". You shouldn't expect anyone else to do...
Depends on environment being used. If Eclipse, use -nl <locale> argument in eclipse.ini to specify the locale. Refer its documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.