英文:
Unable to use #strings.substring to display a portion of the text
问题
我正在使用Spring Boot与Thymeleaf,我将post对象传递给视图解析器。我只想显示帖子内容字段的某个特定部分,就像这样:
示例:
这是来自数据库的列。
____
---------------
内容列
---------------
文本文本文本t
ext文本文本
---------------
所以我想在.html页面中只显示一定数量的字符,就像这样:
您的文本:文本文本te... '阅读更多'
我尝试了以下方法:
<p id="postcontent" th:utext="${#strings.substring(${post.content},0,12)}"></p>
然而,我得到了模板解析错误,然后我尝试了:
<p id="postcontent" th:utext="${#strings.substring(post.content,0,12)}"></p>
这只会打印出整个表达式的全部内容。
L.E 所以,我找到了正确的语法,即:
th:utext="${#strings.substring(post.content,0,17)}"
问题是,如果字符串小于所需长度,我会得到一个错误:
> Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 17
有没有办法解决这个问题?假设这永远不会发生,因为帖子内容永远不会如此短,但我希望有一种方法来处理这个异常。
英文:
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.
答案1
得分: 2
为了理解你的错误,你必须知道什么是字符串。
在Java中,字符串是字符数组:
char[] example = {'H', 'E', 'L', 'L', 'O'};
你可以类似于数组一样操作字符串。数组的第一个索引是0,最大索引是长度 - 1。
现在假设上面的例子是一个字符串实例,你执行以下操作:
example.subString(0, 3) // 返回值将是 "HEL"
example.subString(0, 10) // 抛出 StringIndexOutOfBoundsException 异常
为了防止这种情况,你必须使用首先出现的限制。你的上限是17或字符串长度定义的上限。
example.subString(0, Math.min(17, example.length()));
虽然Thymeleaf中没有内置函数,但你可以使用特殊的 T(...) 运算符来调用静态方法(允许你在Thymeleaf中使用Java的 Math.max(...))。
<div th:with="maxNumber=${T(Math).min(8, 12)}">
<p th:text=${maxNumber}></p>
</div>
如评论中所提到的,还有一种更特殊的方法来实现你的目标:
英文:
To understand your error, you have to know what strings are.
Strings in java are arrays of char:
char[] example = {'H', 'E', 'L', 'L', 'O');
You can manipulate strings in a similar why to arrays. Arrays first index is 0 and max Index length - 1
Let now assume the above example is an instance of String and you do:
example.subString (0, 3) // Return value would be "HEL".
example.subString (0, 10) // StringIndexOutOfBoundsException
To prevent this, you have to use whats ever come first. Your cap at 17 or the cap defined by the length of the string.
example.subString (0, Math.min(17, example.length()));
While there is no built in function in Thymeleaf, you can use the special T(...) operator to call static methods (allowing you to use Java's Math.max(...) in your Thymeleaf).
<div th:with="maxNumber=${T(Math).min(8,12)}">
<p th:text=${maxNumber}></p>
</div>
As mentioned in the comments, there is a more Thymeleaf special method to approach your goal:
https://stackoverflow.com/questions/49897473/i-want-to-abbreviate-a-string-with-thymeleaf
答案2
得分: 1
你还可以使用条件来处理越界情况,例如:
<span th:if="${post.content.length() > 17}">
<span th:utext="${#strings.substring(post.content, 0, 17)}"></span>
</span>
<span th:if="${post.content.length() < 17}">
<span th:utext="${#strings.substring(post.content, 0, post.content.length())}"></span>
</span>
英文:
You can also use a condition to take care of out of bounds e.g.
<span th:if="${post.content.length()>17}">
<span th:utext="${#strings.substring(post.content,0,17)}"></span>
</span>
<span th:if="${post.content.length()<17}">
<span th:utext="${#strings.substring(post.content,0,post.content.length())}"></span>
</span>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论