thymeleaf - 将选择变量表达式* {foo}传递给表达式实用程序对象

时间:2015-12-15 11:47:16

标签: spring thymeleaf spring-el

我有一个弹簧支持的表单,其模型对象Foo包含Bar

<form action="#" th:action="@{/some/action}" th:object="${foo}" method="post">

现在,我想做这样的事情:

<input type="text" id="bar.createdAt" name="bar.createdAt" th:value="*{bar.createdAt} ? ${#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}"/>

但抛出以下解析异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))" (registration:93)
    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:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))" (registration:93)
    org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:161)
    org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154)
    org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
root cause

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 7): Property or field 'createdAt' cannot be found on null
    org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:211)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:43)
    org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:341)

我已经尝试过了: ...#dates.format(*{bar.createdAt}..., ...#dates.format(__*{bar.createdAt}__...但没有一个成功。

同样没有表单(直接访问视图模型对象)工作正常。

知道什么是正确的构造? 在此先感谢!!!

2 个答案:

答案 0 :(得分:2)

而不是:

${#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}

将其更改为:

*{#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}

在您的情况下,实用程序对象将在最后一个选定对象的bar属性上工作,该对象是表单支持对象${foo}

答案 1 :(得分:0)

我认为线索在线:

Property or field 'createdAt' cannot be found on null

您确定bar不是空的吗?