jsp – 如何替代不推荐使用的ExpressionEvaluator

huangapple go评论69阅读模式
英文:

jsp - how to substitute deprecated ExpressionEvaluator

问题

我正在将一个应用程序迁移到JBoss EAP 7.4,该应用程序仍然使用JSP。大多数页面运行正常,但有一个页面让我头疼。该页面使用一个自定义TLD定义,该定义链接到一个类,该类扩展了javax.servlet.jsp.tagext.TagSupport;在doEndTag()方法中的这一点上,该类抛出了一个IllegalArgumentException,因为它无法将String转换为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;}}); //在这里崩溃
dettaglio = (Detail)ob; //无法达到这里

在我的依赖项中,我有javax.servlet-api版本3.0.1(scope=provided),并让JBoss处理其余的依赖项。

现在这个ExpressionEvaluator已经被弃用:

已弃用。从JSP 2.1开始,已被ExpressionFactory替代

ExpressionFactory是抽象的,不提供“eval”方法,所以我不知道如何替换它。应该如何替换它?如何修复这个错误?

谢谢。

英文:

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.

答案1

得分: 0

我认为我找到了一个解决办法:

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

其中 "detail" 是我 JSP 页面中的属性。唯一的区别是,我只需在 JSP 中编写要解析的变量名称,而不是使用 ${} 符号。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月8日 16:19:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定