javajsptaglib

jsp - how to substitute deprecated ExpressionEvaluator


I am porting an application to JBoss EAP 7.4 that still uses JSP. Most of the pages work fine but a page is giving me trouble. The page uses a custom TLD definition which is linked to a class, which extends javax.servlet.jsp.tagext.TagSupport; this class throws an IllegalArgumentException at this point in the doEndTag() method because it can't convert String to Detail:

ExpressionEvaluator eval = pageContext.getExpressionEvaluator();
Object ob = eval.evaluate(detail, Detail.class, pageContext.getVariableResolver(), new FunctionMapper(){public Method resolveFunction(String arg0, String arg1){return null;}}); //Crashes here
dettaglio = (Detail)ob; //does not reach this

In my dependencies I have javax.servlet-api version 3.0.1 (scope=provided) and I let JBoss handle the rest of the dependencies.

Now this ExpressionEvaluator has been deprecated:

Deprecated. As of JSP 2.1, replaced by ExpressionFactory 

ExpressionFactory is abstract and does NOT provide an "eval" method, so I don't know how to replace it. How should it be replaced? How can I fix this error?

Thank you.


Solution

  • I think I found a way around it:

    ExpressionFactory factory = JspFactory.getDefaultFactory().getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory();
    ValueExpression valueExp = factory.createValueExpression(pageContext.getELContext(), "${"+getDetail()+"}", Detail.class);
    dettaglio = (Detail) valueExp.getValue(pageContext.getELContext());
    

    where "detail" is the attribute in my JSP Page. The only difference is that I had to simply write the name of the variable to parse in the JSP instead of using the ${} notation.