anergy
anergy

Reputation: 1384

Facelet Method call where property required

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

Upvotes: 1

Views: 1169

Answers (1)

BalusC
BalusC

Reputation: 1109542

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() {
    // ...
}

Upvotes: 2

Related Questions