Jackson 的 JsonNode 上的 JSP 表达式语言
假设我正在使用 Spring 并且有一个控制器返回一个包含字段 data
的模型,该字段是 杰克逊的 ObjectNode
。在 data
中,我有一个名为 log
的 StringBuilder
。
在我使用的 JSP 中
${data}
,我得到以下输出:
{"log":hello world}
现在,如果我想访问 log
我以为我可以使用,
${data["log"]}
但我得到的只是
javax.el.PropertyNotFoundException: Property 'log' not found on type org.codehaus.jackson.node.ObjectNode
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:191)
javax.el.BeanELResolver.property(BeanELResolver.java:300)
javax.el.BeanELResolver.getValue(BeanELResolver.java:81)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
org.apache.el.parser.AstValue.getValue(AstValue.java:123)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
:
${data.log}
显示相同的。
我知道根据 EL data.log
在 data
上查找方法 getLog()
,但是 IIRC Maps
支持对其密钥的这种访问。有没有类似的方法可以让它与杰克逊一起工作?或者如果这样行不通,你会怎么做?
Let's say I'm using Spring and have a controller that returns a model containing a field data
that is an instance of Jackson's ObjectNode
. Within data
I have a StringBuilder
named log
.
In the JSP I use
${data}
and I get this output:
{"log":hello world}
Now if I want to access log
I thought I could use
${data["log"]}
but all I get is
javax.el.PropertyNotFoundException: Property 'log' not found on type org.codehaus.jackson.node.ObjectNode
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:191)
javax.el.BeanELResolver.property(BeanELResolver.java:300)
javax.el.BeanELResolver.getValue(BeanELResolver.java:81)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
org.apache.el.parser.AstValue.getValue(AstValue.java:123)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
:
${data.log}
shows the same.
I know that according to the EL data.log
looks for a method getLog()
on data
, but IIRC Maps<?,?>
support this kind of access to its keys. Is there any similar way to make it work with Jackson? Or if that just doesn't work this way, how would you do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经有一段时间没有使用 EL 了,所以我猜测了一下...
因为
ObjectNode
没有实现Map
并且没有为“定义属性 getter log”,也许需要采取不同的方法。 (我猜示例 EL 只适用于Map
实例和 beans。)与其将 Jackson 实现放入您的视图(JSP)中,不如填充适当的 Java 结构(没有任何Jackson 类)从 Jackson 结构中的数据,然后只传递查看 Jackson-free Java 结构?然后,将 Java 数据绑定到视图元素会更加简单。
由于 Jackson 的优势之一通常是可以非常轻松地从 JSON 数据填充 Java 结构,因此我认为这种方法相对容易。
I haven't worked with EL in a while, so I'm guessing a bit...
Since
ObjectNode
does not implementMap
and does not define a property getter for "log", perhaps a different approach is necessary. (I'm guessing the example EL only works onMap
instances and beans.)Instead of putting Jackson implementations in your view (the JSP), does it make sense to populate an appropriate Java structure (without any Jackson classes in it) from the data in the Jackson structure, and then just pass the view the Jackson-free Java structure? Then, binding Java data to view elements would be more straightforward.
Since one of Jackson's strengths is generally making it very easy to populate Java structures from JSON data, I'd think this approach would be relatively easy.
现在我重新审视这个问题,我编写了自己的 ELResolver 对于 ArrayNode 和 ObjectNode。有用的链接是
编辑:
就 ArrayNode 而言,它帮助我查看 Tomcat 的 ArrayELResolver.从那里去很容易。
Now that I revisited this issue, I wrote my own ELResolver for ArrayNode and ObjectNode. Helpful links were
EDIT:
As far as the ArrayNode is concerned, it helped me to look at Tomcat's ArrayELResolver. It's pretty easy to go from there.