Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Easy el question

 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just kind of a stupid question but how does el evaluate equality between objects?

Say I have two Integer objects A and B

Does el convert these to strings or int primitives to do comparison

#{A == B}

If I want to compare their intValue's do I need to do this instead

#{A.intValue == B.intValue}

or

#{A.toString == B.toString}

Thanks for any help.
 
Tim Holloway
Bartender
Posts: 18465
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it's a very good question. It's an even better one since the EL for JSTL and the EL for JSF aren't quite the same - though I believe that JSF is aiming for a Unifed EL standard that will make life a lot simpler.

First off, EL processors generally have little patience with general-purpose Java code, so you can pretty well write off explicit conversion method calls.

You might fing this little resource intesting, though it may or may not directly relate to expressions in JSF: http://jakarta.apache.org/commons/el/api/org/apache/commons/el/Coercions.html
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok Thanks for the link. According to it, it will coerce both Integers to Longs but it still does not explain how the comparison is done.

Applying equality operator
A {==,!=} B
if A==B
apply operator
if A or B is null
return false for ==, true for !=
if A or B is BigDecimal, coerce both A and B to BigDecimal and then:
if operator is == or eq, return A.equals(B)
if operator is != or ne, return !A.equals(B)
if A or B is Float or Double
coerce both A and B to Double
apply operator
if A or B is BigInteger, coerce both A and B to BigInteger and then:
if operator is == or eq, return A.equals(B)
if operator is != or ne, return !A.equals(B)
if A or B is Byte,Short,Character,Integer,Long
coerce both A and B to Long
apply operator


This is what I am trying and it is blowing up



This is in the context of a t:inputText Tomahawk component within a DataTable. acctId is a Integer field of the account bean. account is represented by acct as the var in the dataTable.
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I figured out my syntax was not quite right but now I am having this problem

when I use this


this part works
accountBacking.account.acctId.intValue

this part doesn't
acct.acctId.intValue

acct is the row variable from a datatable. I keep getting this error message


[ August 25, 2006: Message edited by: Brian Smith ]
[ August 25, 2006: Message edited by: Brian Smith ]
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I figured it out. I don't need to use intValue because the comparison is done correctly using just the Integer property (acctId). The logic was flawed in my el expression, that is why I started going down this track, thinking the objects were not being compared properly.

It turns out this

displayValueOnly="#{!((accountBacking.editMode == false) and (accountBacking.account.acctId.intValue() == acct.acctId.intValue()))}"

should have been this

displayValueOnly="#{!((accountBacking.editMode == true) and (accountBacking.account.acctId.intValue() == acct.acctId.intValue()))}"
 
Tim Holloway
Bartender
Posts: 18465
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I'd expect the following to work, without having actually testing it:


displayValueOnly="#{!accountBacking.editMode and (accountBacking.account.acctId == acct.acctId)}

Although, in truth, I've found it to be preferable to simply do

displayValueOnly="${accountBaking.displayMe}"

----
in backing bean:
----

public boolean displayMe() {
return (!editMode) && (account.acctId == acct.acctId)
}

You might need to inject the acct object to do that.

Why do I prefer this approach? Less fights with EL, it has proven to make the JSPs more maintainable, and it takes logic off the view and puts it in the controller where it's "supposed" to be (which is probably why I find it more maintainable).
[ August 28, 2006: Message edited by: Tim Holloway ]
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip Tim. In order to inject the acct object could I just use a binding from the DataTable to gain a reference to acct or is there some other way?
 
Tim Holloway
Bartender
Posts: 18465
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Normally you'd just set it up as a managed property in the faces-config.xml.

But if each row has its own acct object, you'll need something to pair them up as the rows are iterated, and that's done at too fine-grained a level for JSF managed properties.

I just realized that displayMe shouldn't act at the backing (Baking??? ) bean level, since the account object varies as you enumerate rows. It should be getting the current row from the model. Normally I'd go further and recommend that displayMe() be a part of the row model class, but you're checking a page-wide property (editMode) as well. Also, as a boolean, that should have been a property (isDisplayableRow() or something).

Let me try again:


Hopefully, I did a better job that time!
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!