Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL: equality test against a static member  RSS feed

 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to test equality between a jsp attribute which contains an instance of a class, and another instance of that class that is a static member of the class.

It's the classic java typedef enum pattern where there are instances of a given class stored as static members of that class:



So in java it would be:



How is the equivalent test done in JSTL?

My usage happens to be a jsp tag file, but the issue is plainly how to do a JSTL test for equality between two instances of a class where one is a static member.



That's what I'm trying to do logically, but of course, it's not possible to embed scriptlet inside an EL test expression.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as you know, the EL is not meant to be a Java replacement, but is geared towards referencing bean-patterned scoped variables; of which your type-safe enumeration is clearly not.

In this case what I would probably do is to (at application startup time, probably using a context listener) is to load a Map into the application context containing the enumerated values keyed by their name.

That way, in an EL expression you can reference them in a somewhat similar manner as in Java code.

Let's say that you name the scoped variable map statusValues. Then a reference could look like:



assuming that you put the Status.CANCELLED enum instance into the map using the key 'CANCELLED'.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that, unlike in Java expressions, the EL == operator is a test for equality, not identity. SO be sure that your enum instances can deal with that and produce the correct result.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick, clear reply. I had a nagging feeling that this was a common need and there had to be a way around it.

I can see how geting the status values into scope in a Map keyed by a String with same name of the static var would do the trick.

For what it's worth, it got me to thinking of another possiblity: add some sugar to the status class that will allow bean-style property access for the various static values. For each static instance in the type-safe enum class, I'd add an accompanying boolean getter:



This bit of convenience api will avoid duplicating the mapping of the names (static var in one place, string key to a map in another) , keeping it all within the model's Status class.

The scriptlet code would then be:



This also then allows other bean-style property access usage (eg: velocity).

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would be another good way to do this. The key is to start thinking, as you have done, in terms of how to structure the data for the page, rather than the other way around. I have a blog entry (which will also appear in this month's JavaRanch Journal, I believe) on this very subject. Just follow the 'Bear's Den' link.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, I like your solution better but didn't know if you were willing to massage the beans themselves or not. The bean solution you came up with is cleaner, and can also be used off-page, as well as negating the equality/identity issue.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction: since the accessor is for a boolean with the isXXX() style, the EL expression within the test shoud have read:



A bit less expressive, perhaps.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I've since tried this on for size and it seems a worthwhile extension to the type-safe enum pattern in cases where a bean-style getter is required. It made the tag code much cleaner, and inverting the responsibility to the model to make the property accessible (where possible) pays off in other ways, too.

Thanks again.
 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can embed scriptlet if you use the RT version.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!