If I understand correctly: you are looking for a way to evaluate the EL expression at run time, which is stored inside another value provided by the EL expression, something like a recursive evaluation of EL.
Since 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; } }
Associated 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>
Using:
<custom:SimpleEval value="${someELexpression}" />
Note:
I tested it with 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 is just a proof of concept.
In my tests, it worked for session variables using ${sessionScope.attrName.prop1} , as well as query parameters using ${param.urlPar1} , but since it uses the current evaluator of the JSP expression, I believe that it should work and for all other "normal" EL expressions.
source share