Rupeal
RSSLinkedInTwitterFacebook

jQuery and Java Server Faces

I honestly love jQuery javascript library, and absolutely am disappointed with JavaServer Faces (JSF).

jQuery is a neat library, that eases the javascript development, by enabling CSS (and other) search capabilities.

For instance if you want to change the background color to red for all items with the class “myClass” you just have to query $(".myClass").css("background", "red");.

I’m not going to go further on jQuery since there are a lot of tutorials out there. My problem was when I was trying to integrate jQuery with a JSF application. JSF as the neat “feature” of generating id’s for all our rendered components based on the view (or subview) which the component is on.

So if I have a <h:inputButton id="button1"/> the generated id will be view:button1. Which causes a main problem when you want to apply CSS styles to this element. You can’t simply add style to #view:button1 because it’s not CSS valid. And for making it worse, you can’t query using jQuery for this element to… so you see where I’m getting at.

The solution I found was to use the j4j Tag Library idProxy to generate a chosenidfor the components we want to manipulate via jQuery. The
j4j idProxygenerates an invisiblespan(I believe it’s label or a span element) with the id we wanted, and a title of the JSF generated id for our component.

So for our later inputButton we would replace with <h:inputButton id="button1"><j4j:idProxy id="myButton"/></h:inputButton> and get a
<span id="myButton" value="view:button1"></span&gt; right beside of our rendered inputButton component.

But still, this is not enough because if I want to manipulate via jQuery the input button we still have no way to access our component! So we need a javascript function, let’s name it jsf() which receive a <j4j:idProxy> generated code>id and returns our JSF component jQuery object!
function jsf(id){
//Finds the j4j Element and retrieves the ID of the target component which is stored on the
title attribute
realId = document.getElementById(id).title;

//Finds the JSF component
var element = document.getElementById(realId);

//Returns an jQuery element based on the DOM element of our target component
return $(element);
}

And voila! Now all we have to do to use jQuery on a JSF web application is to put the j4j:idProxy on every JSF component we want to manipulate and use the chosen id with the jsf() javascript function.

Rui Alves on March 9th, 2007

Software Development

7
 

7 comentários

  • Danny Wachsstock

    11 March 2007

    If you’re going to use document.getElementById, you can use invalid selectors directly; thus
    var element = document.getElementById(‘view:button1′);
    return $(element);

    There is some discussion among the jQuery developers (http://dev.jquery.com/ticket/143) about adding backslash-quoting to allow $(‘#view\:button1′), the way CSS does

     
  • rupeal

    11 March 2007

    Yeap, that’s true, but still you have to know the name of the view/form that the jsf implementation generates.

    So having j4j idProxy actually gives you a final id (independent from the jsf implementation you’re using) that you can use anytime!

     
  • shilpa

    14 April 2007

    As i am not able to use j4j in my application can u plz post the entire example to use this . i mean your jsp and does it require any changes in faces-config….
    I am gettin an error to load the j4j.jar
    it says the resource not found in web.xml …
    can u plz help me.

     
  • shilpa

    18 April 2007

    using j4j i can create a proxy id but if i want to use this id in retrieving each row of a datatable then how can i do that?
    right now i am getting only the first row data into my javascript for manipulation but i want all the rows data.
    can u tell me how to do that?

     
  • rupeal

    19 April 2007

    You can use the javascript function that I’ve posted.

    With the jsf(id) you can access the HTML element using j4j generated id.

    Retrive the value of your element and post it back to retrieve the row you wish. It should be as simple as that!

    Hope it helped!

     
  • Marek

    19 September 2007

    You can use tomahawk instead of standard JSF tags and forceId attribute.

     
  • Bill

    29 January 2008

    Using jquery-1.2.2 referencing elements in the js using formId\\:tagId does work

    for example

    window.onload = init;

    var show_speed = 1500;

    function init(){
    $(“#newData\\:advanced_search”).hide();
    }
    function show_advanced()
    {
    var show_button = document.getElementById(“newData\\:advanced_search_show”);
    $(“#newData\\:advanced_search”).show(show_speed);
    show_button.innerHTML = “Hide Advanced Search“;
    }// end show_advanced method

    function hide_advanced()
    {
    var show_button = document.getElementById(“newData\\:advanced_search_show”);
    $(“#newData\\:advanced_search”).hide(show_speed);
    show_button.innerHTML = “Show Advanced Search“;
    }// end hide_advanced method

    Show Advanced Search

    TEST JAVASCRIPT IN JSF
    Hide Advanced Search

     

Deixe um Comentário