• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check NULL in JSTL

 
Maria Diminuta
Greenhorn
Posts: 15
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maria Diminuta
Greenhorn
Posts: 15
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Diminuta
Greenhorn
Posts: 15
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Maria Diminuta
Greenhorn
Posts: 15
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic