thymeleaf 3 and th:field syntaxe - thymeleaf

I have a form template using thymeleaf 3 and I want to use th:field="*{dueDate}" syntax to bind my model property. I try to do the same as th:value="${#strings.substring(model.dueDate, 0, 19)}" but all my attempts fail.
It is possible to do this into a th:field instead of a th:value ?
EDIT:
I have to substring my date from java.sql.timestamp because it's a little bit different as the format used by my datepicker (2020-01-30 13:00:00.0 instead of 2020-01-30 13:00:00). I understand it isn't a good idea to substring the date string but It is difficult to have a entity method to bind the value as well with th:field. Probably I will use th:value in this case to give me the possibility to create that entity method providing the good format.

Related

Where can I see the specification for thymeleaf th:method?

I've seen a lot of answers about how to send PUT/DELETE/PATCH HTTP requests with thymeleaf, and it's by using th:method = "the_specific_method", but i haven't found the thymeleaf specification about that. Can anyone help showing me where is it?
Thanks in advance.
I've tried to google for the answer, but no luck.
th:method isn't special to Thymeleaf -- it's just like any other plain old attribute which will output the result of an expression to the method attribute. It doesn't do (or care about) anything else. You can put any string and/or string expression into it, and Thymeleaf will happily output it.
th:method="${'the_specific_method'}"
will output
method="the_specific_method"
without regards to whether or not it's valid. If you want to learn about the method attribute, you just need to learn about how method works in plain old regular html and how browsers (and/or Spring) work with it.

How to loop from 'a' to 'z' in thymeleaf?

is there a way to loop from 'a' to 'z' using th:each block of thymeleaf in a similar way we do using #numbers.sequence?
For the moment I am using a static array of chars in the back-and and passing this to the front-end.
You can do the following:
<div th:with="letters='abcdefghijklmnopqrstuvwxyz'"
th:each="num : ${#numbers.sequence(0, 25)}">
<div th:text="${#strings.substring(letters, num, num+1)}"></div>
</div>
This still requires a hard-coded string (as you can see), which you could just as easily pass to Thymeleaf as a Java String (not as an array of chars). But maybe it's more acceptable than what you are currently doing.
I don't know of any way in which Thymeleaf can directly use Java's (char)('A' + num) technique - otherwise that would probably be what you are looking for. I think that is not possible. Unless/until someone proves it is possible.
Update
To prove myself somewhat wrong, I used the following approach:
<div th:each="num : ${#numbers.sequence(97, 122)}">
<div th:text="${#conversions.convert(num, 'java.lang.Character')}"></div>
</div>
This prints a through z by converting the decimal ASCII values to Java chars.
However this is only valid if you are using the Spring dialect of Thymeleaf - which is not mentioned in your question - so may be of no help to you.
A solution using the Thymeleaf standard dialect (no Spring) is to define a custom conversion service.
That requires more coding than your current approach - so again, is probably not what you want. But I mention it just in case. How you implement this depends more specifically on how you have integrated Thymeleaf into your program.

Dynamic variable name with dashes in thymeleaf

I am trying to create a variable name in thymeleaf by using another variable as prefix:
th:with="appOverview = ${__${tabId}__AppOverview}"
It works most of the times, but in the case where tabId contains dashes (i.e., "my-test-tab"), thymeleaf throws the following exception:
The operator 'SUBTRACT' is not supported between objects of type 'null' and 'null'
I guess that the dashes confuse thymeleaf into believing it's an arithmetic operation between non-existing variables.
I tested adding a variable with th:with="my-var='bad dashes'" and it worked fine, so I suppose dashes are generally accepted as characters in variable names.
Is there a way to tackle this issue?
Ah, I finally found it!
The thing I was missing is that all model variables are stored in thymeleaf in the #vars object, so accessing them from there won't confuse thymeleaf. Here's what worked:
th:with="appOverview = ${#vars.get('__${tabId}__AppOverview')}"

Meaning of empty from, empty create etc. in expression language

I came across some .xhtml files where for some components the expression language used was like rendered="#{empty from}", rendered="#{empty create}" etc.
I know that empty is an operator in EL, used to check whether a value is null or empty, but I did not understand the meaning of above mentioned ELs.
Can somebody explain to me what above EL's mean?
The rendered attribute is a signal whether JSF should generate HTML for the component or not. If it evaluates false, then it won't generate HTML for the component (nor for its children). The #{empty form} will evaluate false when the #{form} is not null nor empty.
Simple as that. You can find another examples of boolean expressions here: Conditionally displaying JSF components

Accessing a HashMap using Struts 2

I have hashmap that was created on a page using the struts2 <s:set> tag. It looks something like this
<s:set var="mymap" value="#request.mymap"/>
At some point in the page, i need to get a value from the hashmap based upon a key, and i want to do it using OGNL.
The key is generated based upon some logic, which i store using another <s:set> tag. Something like this
<s:set var="mykey" value="1">
I need to get a value from the hashmap using this key. And I need to display it.
How do I simply call the get function on the hashmap?
I tried this
<s:property value="#mymap[#mykey]"/>
and this
<s:property value="#mymap[%{#mykey}]"/>
and this
<s:property value="%{#mymap[%{#mykey}}]"/>
The third one obviously does not work because of the nesting problem.
But the same nesting logic is applicable to the second case as well, due to the manner the value attribute is handled. However none seem to work for me.
The issue here is that my key is unknown. It is a dynamically generated string based upon some pattern. I need to access the object stored in the hashmap using this dynamic key. And due to the inability of nesting ognl, I am in a fix.
I suppose the issue is very simple. I almost feel that I get it, but somehow the solution eludes me.
I suppose I was using a different version of struts wherein using the %{} was required for the expression to be evaluated. I changed the jar files now.
This is what did the job for me:
<s:property value="#mymap.[#mykey2]"/>
My problem was coming because I was trying to use it in a href for a s:a tag. And without the %{} operator, the expression was not being evaluated.
So, i guess, i was right in the beginning itself. Rest of the time, it was just me being silly. :>
Update:
I wrote a blog post on the issue, in case anyone is interested.
http://mycodefixes.blogspot.com/2010/11/struts-2-creating-and-accessing-maps.html

Resources