1

Is it possible to invoke a method where property is expected in JSF 2.0 Facelet EL. For example:

<h:outputText value="#{pojo.methodName}" />

where pojo is an instance of POJO and methodName is the name of method. An error would be thrown because JSF expects to find getMethodName method. Before someone asks why one would need this, consider any value we want to display in text which is computed and we don't have required getter method and no source code.

Update after BalusC Answer:

No rename possible because no source code available. methodName() didn't work. The only difference is that in actual code its chained pojo.

<h:outputText value="#{pojo1.pojo2.methodName()}" />

Since other properties are working for pojo2, I assume that its methodName which can not be invoked. Server says "The class does not have the property methodName"

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

Empty faces-config

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
   version="2.0">
</faces-config>

P.S. environment JBoss 6 and JSF 2

anergy
  • 1,374
  • 2
  • 13
  • 29

1 Answers1

2

Add parentheses:

<h:outputText value="#{pojo.methodName()}" />

(which only works in EL 2.2, which is part of Java EE 6, so it only works out box in Tomcat 7, Glassfish 3, etc, see also Invoke direct methods or methods with arguments / variables / parameters in EL)

Or just rename the method:

public String getMethodName() {
    // ...
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • No source code, so no rename. I tried that but it says doesn't have property. Only difference with example is that it is chained for mulitple Pojos e.g. pojo1.pojo2.methodName() P.S. environment JBoss 6 and JSF 2 – anergy Dec 06 '11 at 12:57
  • Is the `web.xml` declared conform Servlet 3.0 spec? EL 2.2 requires Servlet 3.0. If lower, it will fall back to exactly that modus. Note that the method must not return `void`. – BalusC Dec 06 '11 at 12:59
  • I put the web.xml header. Is there anything else required for 3.0. – anergy Dec 06 '11 at 13:49
  • No, all looks fine. Again, does that method return non-`void`? What exactly happens when you write the EL as such? Does "nothing" happen or do you get an exception? – BalusC Dec 06 '11 at 13:51
  • PropertyNotFoundException "The class xyz does not have the property methodName" – anergy Dec 06 '11 at 14:17
  • Do you have any `el*.jar` files in `/WEB-INF/lib`? Remove them. JBoss 6 should by itself already supply EL 2.2. Or do you have any custom EL resolvers registered in `web.xml` or `faces-config.xml`? If so, which? – BalusC Dec 06 '11 at 14:19
  • No el*.jar, no custom resolver, I guess I have already taken a lot of your time. Thanks – anergy Dec 06 '11 at 14:39