2013-03-21 75 views
1

這是使用代碼示例解釋的最簡單方法。在某些情況下,我們有大量的文字列表項,但顯示出比前幾個字更是沒有用的,這是我們如何在JSP對付它:使StringTemplate限制字符串長度的最佳方法是什麼?

<% for(Item item : items) { %> 
<li><%=StringHelper.shorten(item.getValue(),30))%></a></li> 
<% } %> 

過濾器有一定的邏輯使其避免切詞,幷包括「...」以指示截斷,等等。即:

<li>Some text</li> 
<li>Some other text that is longer...</li> 

是否有一個乾淨的方式來做到這一點與格式化。我知道我大概可以做這樣的事情,但它似乎有點哈克,我們在不同的地點使用不同的數字:

$items:{i|<li>$i.value;format="max30"$</li>}$ 

回答

0

最簡單的方法就是自定義標籤,例如

public class ShortenTag extends TagSupport{ 

    private static final int MAX_LENGTH = 30; 

    public int doStartTag(){ 

    // shorten string here 
    } 


} 
+0

谷歌搜索「stringtemplate TagSupport」基本上只返回這個帖子。我會看看你提到的這個東西是否有任何信息。 – Jacob 2013-03-21 23:03:11

相關問題