JSF EL: how to pass parameters to value expression?

43 Views Asked by At
@ManagedBean("myBean")
public class MyBean {

    public String getId(String name) {...}
}


#{myBean.id('foo')}

JSF trying to resolve MyBean.getId('foo')(). How to pass parameter to value expression?

JSF 2.2.

Tried: #{myBean.getId('foo')}, not working. JSF tried to resolve MyBean.getGetId('foo')().

1

There are 1 best solutions below

0
Jonathan S. Fisher On

The first issues is the @ManagedBean annotation. If you're on JSF 2.2, you should not be using @ManagedBean as those have been sunset for a few years. Instead, use the modern CDI annotations that have a well-defined scope.

Try the following instead:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class MyBean {

    public String getId(String name) {...}
}

Next with the EL expression, invoke the getId() function like this:

#{myBean.getId('foo')}

You cannot use property-like expressions because the getId(...) method takes an argument and does not follow the JavaBean standard (see here: https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/beans/Introspector.html)

You could theoretically create your own ELResolver that supported this if you wished. (https://subscription.packtpub.com/book/business-&-other/9781782176466/1/ch01lvl1sec13/writing-a-custom-el-resolver)