Jeremiah Atwood
Jeremiah Atwood

Reputation: 153

Evaluating EL expressions in request attributes

We store HTML markup inside XML documents and unmarshall using JiBX. We also use Spring and when one of these objects is added to the model, we can access it in the JSP via EL.

${model.bookshelf.columnList[0].linkList[0].htmlMarkup}

Nothing groundbreaking. But what if we want to store EL expressions in the HTML markup? For example, what if we want to store the following link?

<a href="/${localePath}/important">Locale-specific link</a>

... and display it in a JSP where LocalePath is a request attribute.

Or even more interesting, what if we want to store the following?

The link ${link.href} is in column ${column.name}.

... and display it inside a nested JSP forEach...

<c:forEach var="column" items="${bookshelf.columnList}">
    <c:forEach var="link" items="${column.linkList}">
        ${link.htmlMarkup}
    </c:forEach>
</c:forEach>

These EL expressions in request attributes are never evaluated. Is there a way get them to evaluate? Something like an "eval" tag? Token replacement works in the first example, but not in the second and isn't very robust.

Upvotes: 4

Views: 2344

Answers (2)

Rian
Rian

Reputation: 1333

You should not need to programmatically parse the el expression yourself, your tag should be supplied with the result of the evaluation by including the <rtexprvalue>true</rtexprvalue> in the taglib e.g.

   <tag>   
      <name>mytag</name>

      <attribute>
        <name>myattribute</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
       </attribute>
   </tag>

Upvotes: 0

jCoder
jCoder

Reputation: 2319

If I understand it right: you are looking for a way to evaluate an EL expression at runtime that is stored inside another value provided by an EL expression - something like recursive EL evaluation.

As I could not find any existing tag for this, I quickly put together a proof-of-concept for such an EvalTag:

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.servlet.ServletContext;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleEvalTag extends SimpleTagSupport {
    private Object value;

    @Override
    public void doTag() throws JspException {
        try {
            ServletContext servletContext = ((PageContext)this.getJspContext()).getServletContext();
            JspApplicationContext jspAppContext = JspFactory.getDefaultFactory().getJspApplicationContext(servletContext);
            String expressionStr = String.valueOf(this.value);
            ELContext elContext = this.getJspContext().getELContext();
            ValueExpression valueExpression = jspAppContext.getExpressionFactory().createValueExpression(elContext, expressionStr, Object.class);
            Object evaluatedValue =  valueExpression.getValue(elContext);
            JspWriter out = getJspContext().getOut();
            out.print(evaluatedValue);
            out.flush();
        } catch (Exception ex) {
            throw new JspException("Error in SimpleEvalTag tag", ex);
        }
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

Related TLD:

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>custom</short-name>
  <uri>/WEB-INF/tlds/custom</uri>
  <tag>
    <name>SimpleEval</name>
    <tag-class>SimpleEvalTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>value</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.Object</type>
    </attribute>
  </tag>
</taglib>

Usage:

<custom:SimpleEval value="${someELexpression}" />

Note:

I've tested it using Tomcat 7.0.x / JSP 2.1, but as you can see in the source code, there is no special error handling etc., because it's just some proof-of-concept.

In my tests it worked for session variables using ${sessionScope.attrName.prop1} and also for request parameters using ${param.urlPar1} but as it uses the current JSP's expression evaluator I suppose it should work for all other "normal" EL expressions, too.

Upvotes: 2

Related Questions