waffel’s Weblog

November 28, 2008

how to use EL from java in a JSF 1.2 environment

Filed under: java,JSF,software — Thomas Wabner @ 9:47 pm
Tags: , , ,

There are not much examples, how to use a ELExpression from your java code. The most examples I found, are focussing on „implement your own ELResolver“. The best documentation comes from SUN in form of tutorials and package/javadoc.

Now I will provide some tips how to deal with EL expressions from java. I have tested and implemented this in a JSF 1.2 web application, where I have access to the FacesContext.

In JSF 1.2 you have access to expression factory:

// get application from faces context
Application app = FacesContext.getInstance().getApplication();
ExpressionFactory exprFactory = app.getExpressionFactory();

Now you can use the expression factory to get a MethodExpression or ValueExpression. In my projects we need very often ValueExpressions.

But first you need also an ELContext. There are many implementations available of such context (for JSP, Faces and so on) and enough documentation how to create your own context.
You can access such context again from the faces context:

// getting the ELContext from faces context
ELContext elContext = FacesContext.getInstance().getELContext();
// creating value expression with the help of the expression factory and the ELContext
ValueExpression valExpr = exprFactory.createValueExpression(elContext, "#{devBean.devMode}", Boolean.class);

In this example I will use the boolean value from a „devBean“ to work later with this value. Of course, you can inject the value expression, required class type and so on.
The last needed thing is to assign the value to a local variable:

Boolean developmentMode = (Boolean) valExpr.getValue(elContext);

Thats it!


2 Kommentare »

  1. Thanks for this information.

    Can you clarify the ValueExpression concept? What is its porpuse?

    I have this example code from «JSF 2.0 Cookbook»:


    Check lines 33-35.

    Thanks in advance for your help.

    Kommentar von John Ortiz — Dezember 19, 2012 @ 3:02 pm | Antwort

    • My example is for JSF 1.2 and not 2.0 … pls be aware of those differences, because in JSF 2.0 many things are changed.

      A value expression is the same concept as used on JSP or facelets sites … but in my case I do wanted to use the same expression in JSP/Facelets AND in my java code.

      Kommentar von Thomas Wabner — Dezember 19, 2012 @ 4:56 pm | Antwort

RSS feed for comments on this post. TrackBack URI

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:


Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

Erstelle eine kostenlose Website oder Blog – auf WordPress.com.

%d Bloggern gefällt das: