[英]Best way to make StringTemplate limit the length of a string?
This is easiest to explain using a code example. 使用代码示例最容易解释。 In some scenarios, we have list items that have a lot of text, but showing more than the first few words is not useful, this is how we deal with it in JSP:
在某些情况下,我们的列表项包含很多文本,但是显示的内容多于前几个单词是没有用的,这就是我们在JSP中处理它的方式:
<% for(Item item : items) { %>
<li><%=StringHelper.shorten(item.getValue(),30))%></a></li>
<% } %>
The filter has some logic that makes it avoid cutting words, and including a "..." to indicate truncation, and so on. 筛选器具有一些逻辑,使其可以避免剪切单词,并包括“ ...”以指示截断等。 ie:
即:
<li>Some text</li>
<li>Some other text that is longer...</li>
Is there a neat way to do this with a formatter. 是否有使用格式化程序执行此操作的整洁方法。 I know I could probably do something like this, but it seems a bit hacky, as we use different numbers in different locations:
我知道我可能可以做这样的事情,但是似乎有点不客气,因为我们在不同的位置使用不同的数字:
$items:{i|<li>$i.value;format="max30"$</li>}$
The simplest way is to customize a tag, eg 最简单的方法是自定义标签,例如
public class ShortenTag extends TagSupport{
private static final int MAX_LENGTH = 30;
public int doStartTag(){
// shorten string here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.