【未接迷】Freemarker自定义字符串截取标签
2011-11-22 19:02
302 查看
通过截取字符串,保证英文和中文长度一致 import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Iterator; import java.util.Map; import junit.framework.Assert; import freemarker.core.Environment; import freemarker.template.TemplateDirectiveBody; import freemarker.template.TemplateDirectiveModel; import freemarker.template.TemplateException; import freemarker.template.TemplateModel; import freemarker.template.TemplateNumberModel; import freemarker.template.TemplateScalarModel; /** * 截取字符串 * @author wy * */ public class CutTemplateDirectiveModel implements TemplateDirectiveModel{ /** * 截取字符串 */ private static final String TEXT ="text"; /** * 截取长度,默认为10 */ private static final String LENGTH ="length"; /** * 截取后+的字符串... */ private static final String EXT = "ext"; @SuppressWarnings({ "rawtypes", "unchecked" }) public void execute(Environment env, Map params, TemplateModel[] templateModels, TemplateDirectiveBody body) throws TemplateException, IOException { Iterator<Map.Entry> paramIter = params.entrySet().iterator(); String text = null; String ext=""; int length=10; while (paramIter.hasNext()) { Map.Entry ent = paramIter.next(); String paramName = (String) ent.getKey(); TemplateModel paramValue = (TemplateModel) ent.getValue(); if (paramName.equals(TEXT)) { if (paramValue instanceof TemplateScalarModel) { text = ((TemplateScalarModel) paramValue).getAsString(); } else{ throw new RuntimeException("Value not String"); } } if (paramName.equals(LENGTH)) { if (paramValue instanceof TemplateNumberModel) { length = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); } else{ throw new RuntimeException("Length not int"); } } if (paramName.equals(EXT)) { if (paramValue instanceof TemplateScalarModel) { ext = ((TemplateScalarModel) paramValue).getAsString(); } else{ throw new RuntimeException("Value not String"); } } } Assert.assertNotNull(text); if(body!=null){ Writer out = env.getOut(); out.append(subString(text, length, ext)); body.render(out); } else{ Writer out = env.getOut(); out.append(subString(text, length, ext)); } } private static String subString(String text, int length, String ext) { int textLength = text.length(); int byteLength = 0; StringBuffer returnStr = new StringBuffer(); for(int i = 0; i<textLength && byteLength < length*2; i++){ String str_i = text.substring(i, i+1); if(str_i.getBytes().length == 1){//英文 byteLength++; }else{//中文 byteLength += 2 ; } returnStr.append(str_i); } try { if(byteLength<text.getBytes("GBK").length){//getBytes("GBK")每个汉字长2,getBytes("UTF-8")每个汉字长度为3 returnStr.append(ext); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return returnStr.toString(); } }
然后在FTL模板头部写入
<#assign cut="com.cms.freemarker.CutTemplateDirectiveModel"?new()>
使用方法:<@cut text="CXCCCCCCC中文aaa" length=12 ext='...'/>
具体信息请查看:未解谜
相关文章推荐
- 截取字符串长度的自定义标签
- 标签:截取字符串
- php字符串截取自定义函数
- struts2 标签截取字符串 struts2-在jsp页面中控制只显示前n个字符
- freemarker截取字符串
- PHP基础12-自定义函数截取中文字符串
- 通过引入SiteMesh的JSP标签库,解决Freemarker与SiteMesh整合时,自定义SiteMesh标签的问题
- thinkphp 反字符 去标签 自动加点 去换行 截取字符串
- 按字节长度截取字符串(支持截取带HTML标签的字符串,并自动补全)工具类
- 我也来秀一个FreeMarker自定义的数据分页标签及使用示例
- Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示
- php截取字符串保留完整xml标签
- php自定义截取中文字符串-utf8版
- freemarker截取字符串
- struts2 标签截取字符串 struts2-在jsp页面中控制只显示前n个字符
- php对包含html标签的字符串进行截取的函数分享
- java截取带html标签的字符串并把标签补全(保证格式)
- freemarker自定义标签并生成html静态页面
- 用freemarker做模板创建一个struts2自定义标签
- 截取含HTML标签的字符串