aspose file tools*
The moose likes JSF and the fly likes Check NULL in JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Check NULL in JSTL" Watch "Check NULL in JSTL" New topic
Author

Check NULL in JSTL

Maria Bg
Greenhorn

Joined: Jan 27, 2011
Posts: 4
Hi,

I'm developing a JSF application and at some point in a jsp I need to show one of three possible values of an object, that is:

myObject
+--- subObject1
+--- subObject2
+--- property1

if subObject1 is not null I print subObject1.description, if it's null I check for subObject2, if it's not null I print subObject2.description, otherwhise I print property1

I have this piece of code:


Any ideas?? Should I check with "myObject.subObject != null"??
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

Welcome to JavaRanch Maria!!

Sure you use myObject.subObject ne null (ne is the same that !=) or use not empty, both do the same, but I think you are putting business logic in your view, I suggest you because your're using JSF (And JSF is based in the Model-View-Controller pattern) you should put your bussiness logic in your model, but this is just a suggestion. For example you can check the same in your bean and show the result in your view, for example:
And in your view use
Regards
Cesar


When a dream is ending because to come true - OCPJP 6,7. OCE JPA EE6. MCTS
Maria Bg
Greenhorn

Joined: Jan 27, 2011
Posts: 4
Cesar Loachamin wrote:Welcome to JavaRanch Maria!!

Hehe thanks!!

Cesar Loachamin wrote: but I think you are putting business logic in your view, I suggest you because your're using JSF (And JSF is based in the Model-View-Controller pattern) you should put your bussiness logic in your model, but this is just a suggestion.


I do think the same, but the guy who is managing this project told me to do this way because he doesn't want the java class to be modified from now on... I think it's kind of weird but... he's the boss...


Could it be that it doesn't work because myObject is part of an iteration of objects as follows:

Maria Bg
Greenhorn

Joined: Jan 27, 2011
Posts: 4
Well, I forgot the most important thing... the piece of code I posted doesn't work, that's why I posted it, but forgot to tell that it doesn't work
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Ick. Lose the JSTL. You don't need it and it only causes problems. To conditionally hide an outputText, all that's required is the following extra attribute on the outputText element:


Although this is redundant, since empty text won't show up anyway, even if it's null!


Customer surveys are for companies who didn't pay proper attention to begin with.
Maria Bg
Greenhorn

Joined: Jan 27, 2011
Posts: 4
Yeah, that's the way I solved it... the thing is that I had to check if the object was null because I need to print one of its properties, so if I just write:



it could throw an exception when subObject is null...

Thanks!
 
 
subject: Check NULL in JSTL