Java 在 FreeMarker 中限制字符串长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3394917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:54:06  来源:igfitidea点击:

Limit string length in FreeMarker

javahtmljstlfreemarker

提问by Bart Vangeneugden

I'm trying to get a substring from a string in FreeMarker. However there are 2 thigns to consider:

我正在尝试从 FreeMarker 中的字符串中获取子字符串。但是,有 2 点需要考虑:

  1. The string can be null
  2. The string can be shorter then the maximum string length
  1. 字符串可以为空
  2. 字符串可以比最大字符串长度更短

I do the following:

我执行以下操作:

<#list landingpage1.popularItems as row>
    <li>
        <span class="minititle">
            <#assign minititle=(row.title!"")>
            <#if minititle?length &lt; 27>
                ${minititle}
            <#else>
                ${minititle?substring(0,26)} ...
            <#/if>
        </span>
    </li>
</#list>

I get a freemarker error saying:

我收到一个 freemarker 错误说:

Failed to load templates: Encountered "</#list>" at line 144, column 65 in landingpage1.ftl.
Was expecting one of:
    <ATTEMPT> ...
    <IF> ...
    <LIST> ...
    <FOREACH> ...
    <SWITCH> ...
    <ASSIGN> ...
    <GLOBALASSIGN> ...
    <LOCALASSIGN> ...
    <INCLUDE> ...
    <IMPORT> ...
    <FUNCTION> ...
    <MACRO> ...
    <TRANSFORM> ...
    <VISIT> ...
    <STOP> ...
    <RETURN> ...
    <CALL> ...
    <SETTING> ...
    <COMPRESS> ...
    <COMMENT> ...
    <TERSE_COMMENT> ...
    <NOPARSE> ...
    <END_IF> ...
    <BREAK> ...
    <SIMPLE_RETURN> ...
    <HALT> ...
    <FLUSH> ...
    <TRIM> ...
    <LTRIM> ...
    <RTRIM> ...
    <NOTRIM> ...
    <SIMPLE_NESTED> ...
    <NESTED> ...
    <SIMPLE_RECURSE> ...
    <RECURSE> ...
    <FALLBACK> ...
    <ESCAPE> ...
    <NOESCAPE> ...
    <UNIFIED_CALL> ...
    <WHITESPACE> ...
    <PRINTABLE_CHARS> ...
    <FALSE_ALERT> ...
    "${" ...
    "#{" ...

Very odd. Can anybody help?

很奇怪。有人可以帮忙吗?

采纳答案by Bart Vangeneugden

The error magically solved itself after extensive testing. Must be karma.

经过大量测试后,该错误神奇地自行解决。一定是业力。

My final code for safe checking:

我用于安全检查的最终代码:

<#assign minititle=(row.title!"")>
<#if minititle?length &lt; 27>
${minititle}
<#else>
${minititle?substring(0,26)} ...
</#if>

Hope it helps somebody else

希望它可以帮助别人

回答by RockMeetHardplace

I'm sure you're happy it's working now, but the error you were receiving had nothing to do with your String Truncation Code, it's because your </#if> is incorrect.

我相信你很高兴它现在可以工作,但是你收到的错误与你的字符串截断代码无关,这是因为你的 </#if> 不正确。

<#if condition >
    This Is Correct
</#if>


<#if condition >
    This Will Show An Error
<#/if>

回答by Sandy Xiao

an even easier solution without using if-else

不使用 if-else 的更简单的解决方案

${minititle?left_pad(26)[0..*26]}

${minititle?left_pad(26)[0..*26]}

this will - first insert white space on left to ensure the string is at least 26 char long (if the string is short than 26 char) - truncate string to exact 26 char long (if the string is longer than 26 char)

这将 - 首先在左侧插入空格以确保字符串至少为 26 个字符长(如果字符串短于 26 个字符) - 将字符串截断为精确的 26 个字符长(如果字符串长于 26 个字符)

I've tried and it worked well with VERSION 2.3.24

我试过了,它与版本 2.3.24 配合得很好