views:

40

answers:

3

I was following a tutorial today that had me scratching my head for an hour. Consider:

public class MyClass {
    public int getTotal() {
        amount = 100;
        return amount;
    }
}

and an excerpt from a JSP:

<p>Total: ${objectOfTypeMyClass.total}</p> //object instantiated elsewhere

Nowhere in the code was an instance variable named "total" ever declared or used. The only reference to the word "total" in the whole project (other than in the JSP) was the method getTotal().

So after some desperate last-ditch experimentation, it appears that Expression Language evaluates ${someObject.var} as "call the getVar() method of the someObject object.

I worked with this long tutorial for over a week thinking that ${someObject.var} was saying "directly fetch the saved instance variable "var" from someObject.

Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?

Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".

And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?

What gives?

A: 

This should work:

public class MyClass {

    private int total = 100;

    public int getTotal() {
        return total;
    }

    ...
}
Randall Kwiatkowski
You would probably want a setter method too so that total won't have to be 100 all the time.
Randall Kwiatkowski
+1  A: 

The . in objectOfTypeMyClass.total is the JSTL EL Dot Operator. It can do a few different things. Including:

  1. map.key accessed a value from map stored under key. or
  2. object.property accesses property from object using "JavaBeans" conventions.
z5h
I guess I sort of made an a** out of....well....just me...by assuming that the use of the dot operator in EL is the same as in Java (at least when accessing a variable of an object). Functionally in the end I guess it is (i.e. will give you the desired property), but mechanically I can see that it's not the same under the hood (gets it the JavaBean way). Thanks for the answer z5h
ChrisM
+2  A: 

Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?

That's correct. EL adheres the Javabean specification as described in the EL specification.

Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".

No, it's certainly not case insensitive. It's specified behaviour. ${bean.Total} would not have worked.

And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?

It's because it's supposed to adhere the Javabean specification.

All with all, read the both specifications and everything will be clear :)

See also:

BalusC
I've seen many of your answers on SO BalusC and I'm always impressed not only with the answers, but your willingness to help.
ChrisM
You're right that I couldn't currently tell a Javabean from a hill of beans right now. Thanks for clarifying the behavior. My bean reading is on the long, long list of frameworks/technologies still to learn. Much appreciated.
ChrisM
You're welcome. Concentrate on the Java SE/EE provided ones first. All 3rd party ones like Spring/Struts/etc comes later :)
BalusC