I am using Spring Boot with Thymeleaf, and I'm passing the post object to the view resolver. I only want to display a certain portion of the post.content field, meaning something like this
example:
This is the column from the database.
____
---------------
content column
---------------
text text text t
ext text text
---------------
So I want to display only a certain number of characters in the .html page like here:
Your text: text text te... 'read more'
I have tried
<p id="postcontent" th:utext="${#strings.substring(${post.content},0,12)}"></p>
However, I get a template parsing error and then I tried
<p id="postcontent" th:utext="${#strings.substring(post.content,0,12)}"></p>
This just prints the whole content of the expression.
L.E So, I found the correct syntax which is
th:utext="${#strings.substring(post.content,0,17)}"
The issue now is that, if the string is smaller than the desired length, I get a
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 17
Is there a way to fix this? Lets say this will never happen because there will never be a post content with such a short length, but I want to have a way to manage this exception.