Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thymeleaf - passing a Selection Variable Expression *{foo} to an Expression Utility Object

I have a spring backed form with a model object Foo that contains Bar

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

now, I would like to do something like this:

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

but the following parsing exeption is thrown:

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)

I've already tried: ...#dates.format(*{bar.createdAt}..., ...#dates.format(__*{bar.createdAt}__...but none has worked out.

the same without the form (directly accessing view model object) works fine.

Any idea what would be the right construct? Thanks in advance!!!

like image 202
mooor Avatar asked May 18 '26 20:05

mooor


1 Answers

Instead of :

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

Change it to :

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

So that the utility object will work on the bar property of the last selected object which is the form backing object, ${foo}, in your case.

like image 83
Bnrdo Avatar answered May 20 '26 11:05

Bnrdo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!