Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:59:41+00:00 2026-06-01T02:59:41+00:00

I don’t really understand how variables work in javax.el: // Implemented by the EL

  • 0

I don’t really understand how variables work in javax.el:

// Implemented by the EL implementation:
ExpressionFactory factory = ExpressionFactory.newInstance();

// Implemented by the user:
ELContext context = ...;

Object result = factory.createValueExpression(context1, "${foo.bar}", Object.class).getValue1(context);

Why is it needed to pass the context twice. Is it possible to pass two different contexts? Which is used for what purpose? What is the expected result of:

ValueExpression expr = factory.createValueExpression(context1, "${foo.bar}", Object.class).getValue(context2);

The javadoc of ExpressionFactory#createValueExpression /JSR-245 explains that:

The FunctionMapper and VariableMapper stored in the ELContext are used to resolve
functions and variables found in the expression. They can be null, in which case
functions or variables are not supported for this expression. The object returned
must invoke the same functions and access the same variable mappings regardless
of whether the mappings in the provided FunctionMapper and VariableMapper
instances change between calling ExpressionFactory.createValueExpression()
and any method on ValueExpression.

Furthermore “JSR-245 2.0.7 EL Variables” explains:

An EL variable does not directly refer to a model object that can then be resolved
by an ELResolver. Instead, it refers to an EL expression. The evaluation of that
EL expression gives the EL variable its value.

[...]

[...] in this [...] example:
<c:forEach var=“item” items=“#{model.list}”>
    <h:inputText value=“#{item.name}”/>
 </c:forEach>

While creating the “#{item.name}” expression, the “item” variable is mapped (in the VariableMapper) to some ValueExpression instance and the expression is bound to this ValueExpression instance. How is this ValueExpression created and how is it bound to different elements of “model.list”? How should this be implemented? It is possible to
create the ValueExpression once and reuse it for each iteration:

 <!-- Same as above but using immediate evaluation -->
 <c:forEach var=“item” items=“${model.list}”>
    <h:inputText value=“${item.name}”/>
 </c:forEach>

ValueExpression e1 = factory.createExpression(context,"#{model.list}");
variableMapper.setVariable("item", ??);
ValueExpression e2 = factory.createExpression(context,"#{item.name}");
for(Object item : (Collection<?>) e1.getValue(context)) {
   ??
}

Or is it necessary to create a new ValueExpression for iteration:

ValueExpression e1 = factory.createExpression(context,"#{model.list}");        
for(Object item : (Collection<?>) e1.getValue(context)) {
   variableMapper.setVariable("item", factory.createValueExpression(item,Object.class));
   ValueExpression e2 = factory.createExpression(context,"#{item.name}");
   Object name = e2.getValue(context);
   ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:59:43+00:00Added an answer on June 1, 2026 at 2:59 am

    Why is it needed to pass the context twice. Is it possible to pass two
    different contexts? Which is used for what purpose?

    The variableMapper and functionMapper are only used at parse time (factory.createMethod(…)) while the elResolver is used during evaluation (expr.getValue(…)). Using different contexts to parse and evaluate an expression is fine.

    While creating the “#{item.name}” expression, the “item” variable is
    mapped (in the VariableMapper) to some ValueExpression instance and
    the expression is bound to this ValueExpression instance. How is this
    ValueExpression created and how is it bound to different elements of
    “model.list”? How should this be implemented?

    First of all: forget about “variables”. As explained in the Javadoc, their values are expressions, provided at parse time. Think of EL variables as constants or macros. You cannot redefine them, they are “burned” into the expression.

    What you need is EL’s mechanism to resolve properties. In the following working example, I’m using an ELContext implementation from JUEL, just to keep things simple and focus on the relevan EL usage:

    import java.util.Arrays;
    import java.util.List;
    
    import javax.el.ELContext;
    import javax.el.ExpressionFactory;
    import javax.el.ValueExpression;
    
    import de.odysseus.el.util.SimpleContext;
    
    public class Sof9404739 {
      /**
       * Sample item
       */
      public static class MyItem {
        String name;
        public MyItem(String name) {
          this.name = name;
        }
        public String getName() {
          return name;
        }
        public void setName(String name) {
          this.name = name;
        }
      }
    
      /**
       * Sample model
       */
      public static class MyModel {
        List<MyModel> list;
        public List<?> getList() {
          return list;
        }
        public void setList(List<MyModel> list) {
          this.list = list;
        }
      }
    
      /**
       * EL expression factory
       */
      static ExpressionFactory factory = ExpressionFactory.newInstance();
    
      public static void main(String[] args) {
        /**
         * Simple EL context implementation from JUEL
         */
        ELContext context = new SimpleContext();
    
        /**
         * Define the two expressions
         */
        ValueExpression listExpr =
          factory.createValueExpression(context, "#{model.list}", List.class);
        ValueExpression nameExpr =
          factory.createValueExpression(context, "#{item.name}", String.class);
    
        /**
         * This looks like a variable, but it isn't - it's a "root property"
         */
        context.getELResolver().setValue(context, null, "model", new MyModel());
    
        /**
         * Just for fun, initialize model's list property via EL, too
         */
        context.getELResolver().setValue(context, "model", "list",
            Arrays.asList(new MyItem("alice"), new MyItem("bob")));
    
        /**
         * Evaluate expressions like c:forEach
         */
        for (Object item : (List<?>) listExpr.getValue(context)) {
          /**
           * For each item, define the "item" (root) property
           */
          context.getELResolver().setValue(context, null, "item", item);
          System.out.println(nameExpr.getValue(context)); // "alice", "bob"
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
don't know better title for this, but here's my code. I have class user
Don't really know how to formulate the title, but it should be pretty obvious
I don't understand the meaning of $:<< . in Ruby. I upgraded Ruby to
I don't really know too much about core JavaScript, just a dot of jQuery.
Don't think that I'm mad, I understand how php works! That being said. I
Don't know why people do not practice AJAX implementation for authentication systems. Is it
Don't quite understand why this copy constructor is not invoked when I build with
Don't know a whole lot about streams. Why does the first version work using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.