无法使用#strings.substring来显示文本的部分内容。

huangapple go评论77阅读模式
英文:

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 = {&#39;H&#39;, &#39;E&#39;, &#39;L&#39;, &#39;L&#39;, &#39;O&#39;);

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 &quot;HEL&quot;. 

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).

&lt;div th:with=&quot;maxNumber=${T(Math).min(8,12)}&quot;&gt;
   &lt;p th:text=${maxNumber}&gt;&lt;/p&gt;
&lt;/div&gt;

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.

    &lt;span th:if=&quot;${post.content.length()&gt;17}&quot;&gt;
    &lt;span th:utext=&quot;${#strings.substring(post.content,0,17)}&quot;&gt;&lt;/span&gt;
    &lt;/span&gt;
    &lt;span th:if=&quot;${post.content.length()&lt;17}&quot;&gt;
    &lt;span th:utext=&quot;${#strings.substring(post.content,0,post.content.length())}&quot;&gt;&lt;/span&gt;
    &lt;/span&gt;

huangapple
  • 本文由 发表于 2020年10月26日 00:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64526015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定