JSF Mini Book – Tips, concepts and good practices

18

Posted on September 2, 2012 by

Share it now!

Avoiding the use of logic in the xhtml page

Usually the pages that the user access has some kind of restriction to some data through administrator or manager profiles; those restrictions are called “View Rules” and it will define which behavior the component will have like: to limit the information that the user can have access, show a message or block a button.

To the JSF components there is an option named rendered that is quite similar to the visible option of other languages components. Just set the “rendered=false” and the component will not be displayed to the user.

With JSF it is very easy to access a value of the ManagedBean; sometimes a developer might be tempted to do a code like this: rendered=#{personMB.total == 5}.It is easy to understand and to read; we know that the component will be rendered only if the total of persons is equal 5. Imagine now if this code is used in 15 more components and the if condition should change to: rendered=#{personMB.total == 6}. How hard would be to change this piece of code in 15 components, or even more?

It always a good practice to leave the View Rules in the ManagedBean. It is required just to create a method that returns a Boolean. The code below shows how to do it. To use the code below in the xhtm page just write it like: rendered=#{personMB.totalReached}.

public boolean isTotalReached(){
    return total == 6;
}

Response to JSF Mini Book – Tips, concepts and good practices

  1. Arslan Afzal

    With your code lines

    and

    eclipse was giving error that:

    Multiple annotations found at this line:
    - Invalid location of text (“) in tag ().
    - Invalid location of text (=”) in tag ().
    - Element type “h:outputStylesheetlibrary” must be followed by either attribute specifications, “>”
    or “/>”.

    So I’ve corrected it by changing lines to

    But I think as you mentioned in case of versions of resources, it will automatically picks up latest version. but the code I’m using will not control the version. It looks like hard-coded.

    What you say about it?

    Moreover, You wrote:
    “To use a mapped library by the ResourceHandler let us create a folder named “index.xhtml” inside the folder “/WebContent”.”

    I think you have not created a folder name “index.xhtml” but you have created a file named “index.xhtml”. If I’m right then you need to correct it. :)

    • uaihebert Post author

      Hello Arslan, how are you?

      Unfortunately the xml are not displayed here. =/

      Could you send me an email with the code and the texts that you wrote?

      Thanks for your support and passing by.

      My email is contato (at] uaihebert (d0T) com

      Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *