title: Variables in javax.el API
body: <p>I don't really understand how variables work in javax.el:</p>
  
  <pre><code>// 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);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>ValueExpression expr = factory.createValueExpression(context1, "${foo.bar}", Object.class).getValue(context2);
  </code></pre>
  
  <p>The javadoc of ExpressionFactory#createValueExpression /JSR-245 explains that:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Furthermore "JSR-245 2.0.7 EL Variables" explains:</p>
  
  <pre><code>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:
  &lt;c:forEach var=“item” items=“#{model.list}”&gt;
      &lt;h:inputText value=“#{item.name}”/&gt;
   &lt;/c:forEach&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> &lt;!-- Same as above but using immediate evaluation --&gt;
   &lt;c:forEach var=“item” items=“${model.list}”&gt;
      &lt;h:inputText value=“${item.name}”/&gt;
   &lt;/c:forEach&gt;
  
  ValueExpression e1 = factory.createExpression(context,"#{model.list}");
  variableMapper.setVariable("item", ??);
  ValueExpression e2 = factory.createExpression(context,"#{item.name}");
  for(Object item : (Collection&lt;?&gt;) e1.getValue(context)) {
     ??
  }
  </code></pre>
  
  <p>Or is it necessary to create a new ValueExpression for iteration:</p>
  
  <pre><code>ValueExpression e1 = factory.createExpression(context,"#{model.list}");        
  for(Object item : (Collection&lt;?&gt;) e1.getValue(context)) {
     variableMapper.setVariable("item", factory.createValueExpression(item,Object.class));
     ValueExpression e2 = factory.createExpression(context,"#{item.name}");
     Object name = e2.getValue(context);
     ...
  }
  </code></pre>