Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If-Else JSF question  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 115
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am very new in JSF. I was wondering, how to make an if-else statement. I searched online but as I read different approaches I became even more confused.
I want to write something like this:


How do I write this??

Thanks in advance
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first, you don't attempt to access values such as "bindings.Names". That would annoy the EL processor because property names should not begin with an upper-case letter per the JavaBean paradigm.

Secondly, you're putting logic on what's supposed to be template data, not code. That's not a good idea. Often it doesn't work, and even if it does, it makes the app harder to maintain because you have to look in more than one place for logic: the backing bean and the template instead of just in the backing bean.

So you've basically got 2 options:

1. You can set up 2 different controls and select which one to render:


2. Or you can construct the selectItem list based on what you want to show.

3. Well, you could also do fancy things with EL based on keeping a collection of different SelectItem lists, but that's generally going to be a bit much.
 
Ioanna Katsanou
Ranch Hand
Posts: 115
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
1. You can set up 2 different controls and select which one to render:





Hello, I still have not managed to get it right.. The case I want is as follows: The showAll Names shows all names of a user. If  showAllNames contains "Alice" then I want to select bindings.names.items.
If Show All Names does not contain "Alice" I want to select the bindings.lastNames.items.

I tried this solution. The not in the beginning did not work for me so I changed it to:




But although it enters in the second rendered it never enters the first. I tried




But nothing worked..

Then I wrote
and it entered for Alice the second bindings. Why is this happening?


 
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations! You have just discovered why You Should Not Code Complex Expressions in EL.

Because they are a  to debug!

Always do your complex logic in a backing bean. It's not totally impossible to run an EL expression through a debugger, but it's extremely difficult. Much, much easier when the code is straight Java code in a Java class.

That's why I wrote:


Because then I can do this, in the bean named "bindings":


I had to rename your map, since it had the same name as my boolean property. And I split the code so that a debugger could breakpoint and display the value. Which is something I often do just as a precaution in my own code.
 
Ioanna Katsanou
Ranch Hand
Posts: 115
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !! thanks for all the help..
So I put all the code inside the Bean

when I wrote:



It worked !!! But I had only this.
When I added:




again it did not work  :mad:

Then I tried



and it didn't work either 
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "didn't work". Did both combo boxes display or did neither of them display? Or.....???
 
Ioanna Katsanou
Ranch Hand
Posts: 115
1
Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Define "didn't work". Did both combo boxes display or did neither of them display? Or.....???


When rendered is true it does not display.
When rendered is false the box displays correctly
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have a "the" box. You have two boxes.

In the absence of more precise information, I'll have to assume that the control with ID "lastNameSelect" is the one being rendered.

JSF has the characteristic that if something does not exist, it won't display an error unless it cannot find some other way to deal with the problem.

In particular, if a backing bean lacks a property, that property will not cause an error, it will be assumed to be zero, false, or null, depending on data type. If the property is a class object and your EL references a property within that object, then a NullPointerException will be thrown, since the parent property cannot be found for the child property, but otherwise it will be silently ignored.

So, in other words, my first suspicion is that your property is not being properly exposed to JSF and therefore JSF assumes that its value is always false.

For the property to be seen, there must be a property-fetch method. The EL processor is expecting to be working with JavaBeans, and a good JavaBean doesn't have public member variables, only get/set methods.

Furthermore, the property-get method must have a very specific signature and name.

1. The "get" method must be declared public

2. The "get" method must not take any arguments (exception: a collection property may take exactly one index argument)

3. The "get" method must have a name that begins with "get" or "is". For non-boolean get methods, it should always be "get". For boolean properties, the recommended beginning is "is", but "get" should work. Use only 1.

4. The "get" method must return a non-void data type

5. The "get" method's upper/lower-case rules must be observed. The "get" or "is" must be lower-case. The first letter of the property name must be Upper case, but the property name itself must begin with a lower-case letter.

In other words: "allNames" is a valid property name, but "AllNames" is not. The associated "get" method would be "public xxxx getAllNames()" or "public boolean isAllNames()" - xxxx being a valid Java primitive or class data type.

You should be able to verify proper functioning of the property-fetch method on your View Template. In JSF 2, code a line like this:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!