如何在由thymeleaf解析的html中获取jackson ObjectNode的值

时间:2014-06-12 10:37:12

标签: spring jackson thymeleaf

我是百里香的新手。当我使用百里香和jaskcon时,我有以下问题。 在服务器端,我有以下代码:

List<ObjectNode> fixTimes = new ArrayList<ObjectNode>();
ObjectNode timeNode = objectMapper.createObjectNode();
timeNode.put("startTime", "2013-4-1");
timeNode.put("endTime", "2013-5-1");
fixTimes.add(timeNode);

在我的客户端,我有以下代码:

<tr width="100%" th:each="fixTime: ${fixTimes}">
   <td>
      <input type="text" data-dojo-type="dijit/form/ValidationTextBox" th:value="${fixTime['startTime']}" style="width:100%; height:100%; border:0"/>
   </td>
   <td>
      <input type="text" data-dojo-type="dijit/form/ValidationTextBox" th:value="${fixTime['endTime']}" style="width:100%; height:100%; border:0"/>
   </td>
     <td style="cursor:pointer" onclick="removeRow(this);">-</td>
</tr>

但是当我运行我的程序时,我会收到一条错误消息:

type Exception report

message Request processing failed; nested exception is    org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL  expression: "fixTime['startTime']" (sysnode:143)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested  exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating  SpringEL expression: "fixTime['startTime']" (sysnode:143)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "fixTime['startTime']" (sysnode:143)
org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:153)
org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:149)
org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:103)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:133)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:120)
org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:67)
org.thymeleaf.spring4.processor.attr.SpringValueAttrProcessor.getTargetAttributeValue(SpringValueAttrProcessor.java:71)
org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59)
org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:61)
org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1016)
org.thymeleaf.dom.Node.processNode(Node.java:971)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.Document.process(Document.java:93)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 root cause

 org.springframework.expression.spel.SpelEvaluationException: EL1027E:(pos 7): Indexing into type 'com.fasterxml.jackson.databind.node.ObjectNode' is not supported
org.springframework.expression.spel.ast.Indexer$PropertyIndexingValueRef.getValue(Indexer.java:421)
org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:84)
org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:103)
org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:100)
org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:138)
org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:149)
org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:103)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:133)
org.thymeleaf.standard.expression.Expression.execute(Expression.java:120)
org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:67)
org.thymeleaf.spring4.processor.attr.SpringValueAttrProcessor.getTargetAttributeValue(SpringValueAttrProcessor.java:71)
org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59)
org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:61)
org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1016)
org.thymeleaf.dom.Node.processNode(Node.java:971)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:672)
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:655)
org.thymeleaf.dom.Node.processNode(Node.java:990)
org.thymeleaf.dom.Document.process(Document.java:93)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

如何避免此错误?请帮助我,任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:1)

当使用thymeleaf解析html时,'fixTime'将被视为java对象,因此需要使用方法'get'来获取值。 以下是正确的代码:

  <tr width="100%" th:each="fixTime: ${fixTimes}">
     <td>
        <input type="text" data-dojo-type="dijit/form/ValidationTextBox" th:value="${fixTime.get('startTime').textValue()}" style="width:100%; height:100%; border:0"/>
     </td>
     <td>
        <input type="text" data-dojo-type="dijit/form/ValidationTextBox" th:value="${fixTime.get('endTime').textValue()}" style="width:100%; height:100%; border:0"/>
     </td>
     <td style="cursor:pointer" onclick="removeRow(this);">-</td>
  </tr>

答案 1 :(得分:0)

我猜是的 Indexing into type 'com.fasterxml.jackson.databind.node.ObjectNode' is not supported 是问题。 SpEL不支持通过[]为jackson ObjectNode访问。