tags:

views:

74

answers:

3

So we all know that #{someBean.value} will try and get the content of some property on 'someBean' called 'value'. It will look for getValue(). However, what if this property is boolean? It will look for isValue(). What it won't look for is hasValue().

This got me thinking, what exactly does it do?

this: http://download.oracle.com/docs/cd/E17477_01/javaee/5/tutorial/doc/bnahq.html

refers to PageContext.FindAttribute. PageContext sends you to JSPContext. None of them actually explain the rules they are following to determine the name of the method they are looking for.

It is also fairly easy to find documentation that says the method names must begin with get. However, I know that isValue() works.

Can anyone point me to documentation where this is written down. I'm not looking for tutorials or examples I'm looking for reference.

+1  A: 

NOT ANSWER: try to put in class 2 methods: String getA() and Boolean isA. and then do h:outputText value="#{bean.a}"
You can also play with #{bean.a ? 'true' : 'false'} to define if method is called depending on context.
I'd check myself, but i can't do it right know.

foret
+3  A: 

Basically what you've stated is all there is to it. EL expects the object to follow regular java bean standards. These 2 should help:

http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm

http://download.oracle.com/docs/cd/E17477_01/javaee/1.4/tutorial/doc/JSPIntro7.html#wp71019

Java Drinker
Thanks, this certainly helps.
Jasper Floor
+1  A: 

It's authoratively documented in both the JavaBean Specification (pick the PDF) and EL Specification.

To take the boolean property as an example, it's described in chapter 8.3.2 of JavaBean spec:

8.3.2 Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method.

In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value.

An example boolean property might be:

    public boolean isMarsupial();
    public void setMarsupial(boolean m);

And in chapter 1.18.5 of EL spec:

1.18.5 Coerce A to Boolean

  • If A is null or "", return false
  • Otherwise, if A is a Boolean, return A
  • Otherwise, if A is a String, and Boolean.valueOf(A) does not throw an exception, return it
BalusC
Exactly what I was looking for. Thank you. I had been under the impression that using hasX was valid for a boolean. It is a fairly standard convention but I guess Java(beans) has to draw the line somewhere.
Jasper Floor