Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java expression language resolve boolean attributes? (in JSF 1.2)

Tags:

jsp

boolean

jsf

el

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?

Java EE 5 tutorial chapter - Unified Expression Language 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.

like image 403
Jasper Floor Avatar asked Jul 13 '10 12:07

Jasper Floor


People also ask

What is an expression language in JSF?

JSF - Expression Language. JSF provides a rich expression language. We can write normal operations using #{operation-expression} notation. Following are some of the advantages of JSF Expression languages. Can reference bean properties where bean can be an object stored in request, session or application scope or is a managed bean.

What is an expression language in Java?

An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar}for a nested property.

What is the Unified Expression Language in JavaBeans?

To summarize, the new, unified expression language allows page authors to use simple expressions to perform the following tasks: Dynamically read application data stored in JavaBeans components, various data structures, and implicit objects Dynamically write data, such as user input into forms, to JavaBeans components

How to access a bean using JSP expression language?

For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar}for a nested property. The testattribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean named cartwith 0:


2 Answers

It's authoritatively documented in both the JavaBeans Spec and EL Specification.

To take the boolean property as an example, it's described in chapter 8.3.2 of JavaBeans 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);

So, #{bean.marsupial} expects exactly the above getter/setter pair.

And in chapter 1.23.5 of EL spec:

1.23.5 Coerce A to Boolean or boolean

  • If A is null and the target type is not the primitive type boolean, return null
  • 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
  • Otherwise, error

See also:

  • javax.el.PropertyNotFoundException: Property 'foo' not readable on type java.lang.Boolean
  • Java: how to name boolean properties
like image 133
BalusC Avatar answered Nov 16 '22 04:11

BalusC


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://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html#wp71019
like image 45
Java Drinker Avatar answered Nov 16 '22 02:11

Java Drinker