Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing equality condition in JSTL  RSS feed

 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need jstl and jsp help.

1. I have a variable in my Constants.java --> long CUST_STATUS = 2

2. I have a POJO, Customer
private Long custStatusId;

Now I need to display a button on the screen only if Customer.custStatusId != 2

Your help is greatly appreciated.

Doesn't work
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your EL syntax is wrong. It should be like
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constants aren't directly addressable by the EL. See this article for a way around that.
 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${bean.property != anotherbean.property}

did not work. Link contains lot of details. Can any body suggest a simple solution. I thought this is the simple problem.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either the types doesn't match as you'd expect, or you haven't read Bear's reply.
 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get what you mean by:
Constants aren't directly addressable by the EL.


Constants.java --> long CUST_STATUS = 2
POJO, Customer
private Long custStatusId;
I need to compare customer.custStatusId with Constants.CUST_STATUS for not equality. Are you saying that it can't be done directly without following the details of the link?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what Bear said, yes.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it were any easier, I wouldn't have had to write all that code and write the article!

Granted, mine is a general solution. For a one-off, you can simple wrap the constant in a bean property, or establish it in application context at context initialization.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easiest and best way is to use JSTL´s fmt taglib for it. Define key-value pairs in a properties file and use fmt:setBundle to load it in the scope. You can access it in EL the usual way. This also keeps your Java classes free from cluttered constant values (and makes maintenance more easy --no need to recompile on change of a constant and so on).

Alternatively you can create a bean class extending HashMap, set all constants in the constructor and create/access the bean using jsp:useBean, roughly as Bear´s article shows. There may be a lot of wording in it, but in fact it isn´t that hard.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The limitation with the properties file approach (which I do use for configurable string properties) is that it's contrained to string values. Also, sometimes it just makes sense to use "class constants" as opposed to properties.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I mean that, "class constants' as in "not for presentation".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!