I'm no expert on EL, but I suspect this is because the ELResolver provided by CDI is only required to resolve names to beans. It's designed to be used by other technologies which use EL (such as Jakarta Faces) to integrate with CDI, so it only handles the parts
that are specific to CDI.
Here are some basic examples of using the resolver to look up a bean, and using it indirectly to evaluate expressions. (These may not be
good examples since I'm no EL expert, and we wouldn't expect users to do this, they'd include expressions in their faces files and the CDI ELResolver would be automatically invoked to resolve names to CDI beans.)
Regards,
Andrew
@WebServlet("/el")
public class ELTestServlet extends HttpServlet {
@Inject
private ELAwareBeanManager bm;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ELManager elManager = new ELManager();
// Look up Greeter using the ELResolver directly
Greeter greeter = (Greeter) bm.getELResolver().getValue(elManager.getELContext(), null, "greeter");
// Now we can call greeter ourselves:
resp.getWriter().println(greeter.hello("My Application"));
// Now add the resolver to the manager so that it's used when we evaluate expressions
elManager.addELResolver(bm.getELResolver());
// Evaluate ${greeter.hello} as a value, which results in calling Greeter.getHello()
String helloValue = ELManager.getExpressionFactory()
.createValueExpression(elManager.getELContext(), "${greeter.hello}", String.class)
.getValue(elManager.getELContext());
resp.getWriter().println("helloValue = " + helloValue);
// Evaluate ${greeter.hello} as a method and then call it, resulting in the call Greeter.hello("ELAwareBeanManager")
String personalHelloValue = (String) ELManager.getExpressionFactory()
.createMethodExpression(elManager.getELContext(), "${greeter.hello}", String.class, new Class[] { String.class })
.invoke(elManager.getELContext(), new Object[] { "ELAwareBeanManager" });
resp.getWriter().println("personalHelloValue = " + personalHelloValue);
}
@ApplicationScoped
@Named("greeter")
public static class Greeter {
public String getHello() {
return "Hello!";
}
public String hello(String name) {
return "Hello " + name;
}
public String toString() {
return "I am Greeter";
}
}
}
From: cdi-dev <cdi-dev-bounces@xxxxxxxxxxx> on behalf of hantsy bai via cdi-dev <cdi-dev@xxxxxxxxxxx>
Sent: 12 July 2025 17:23
To: cdi-dev@xxxxxxxxxxx <cdi-dev@xxxxxxxxxxx>
Cc: hantsy bai <hantsy@xxxxxxxxx>
Subject: [EXTERNAL] [cdi-dev] How to use ElAwareBeanManager to evaluate the EL result
Hi I check the new ELAwareBeanManager, the new method getELResovler() can not evaluate the _expression_ directly. I have tried to use invoke to call a method of a bean like this. ELManager elManager = new ELManager(); var resolvedResult = beanManager. getELResolver(). invoke(elManager. getELContext(),
Hi
I check the new ELAwareBeanManager, the new method getELResovler() can not evaluate the _expression_ directly.
I have tried to use invoke to call a method of a bean like this.
ELManager elManager = new ELManager(); var resolvedResult = beanManager.getELResolver().invoke(elManager.getELContext(), elGreeter, "hello", new Class[]{String.class}, new String[]{"ELAwareBeanManager"});
Here, elGreeter is an injected bean; it has a method called `hello` that accepts a String as a parameter and returns a String.
This resolvedResult is null. Not the expected string: Hello, ELAwareBeanManager
1. Is there a simple way to evaluate an _expression_ on the CDI side?
2. I found all global instances in EL that can be application-scoped beans not exposed, eg, ElContext, ElManager, and ExpressionFactory, etc.
Unless otherwise stated above:
IBM United Kingdom Limited
Registered in England and Wales with number 741598
Registered office: Building C, IBM Hursley Office, Hursley Park Road, Winchester, Hampshire SO21 2JN
|