Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 改写JSTL标签
【标  题】:改写JSTL标签
【关键字】:JSTL
【来  源】:http://blog.csdn.net/djy1135/archive/2006/12/18/1448039.aspx

改写JSTL标签

Your Ad Here  
在使用JSTL标签的截取字符串函数时有点不爽!当汉字、字母、数字混合在一个字符串中进行统计长度或截取时,它会把汉字、字母、数字作为同一长度进行截取!如:
String str = “abc中国123”;
${fn:substr(str,0,6)} 的结果就是:abc中国1
这让我们用起来很不舒服,特别是在进行列表时,截取同样的长度,如果哪个包含的字母或数字较多,就会看起来特别短。
为此,我把JSTL源代码下载下来,增加了一个函数getLimitStr(String,int).这个函数的作用是对str进行前len个长度的字符,如果str的长度大于len,则用”…”结尾。
 
具体步骤:
一、在org.apache.taglibs.standard.functions包的Functions.java类中加如下代码:
publicstatic String getLimitStr( String str,int len){
        try{
            if(str==null)     return"";         
            int counterOfDoubleByte = 0;
            byte[] b = str.getBytes("gb2312");
            
            if(b.length <= len)
               return str;
            
            for(int i = 0; i < len-3; i++){           
                if(b[i] < 0)
                 counterOfDoubleByte++;
           }   
            if(counterOfDoubleByte % 2 == 0)
               returnnew String(b, 0, len - 3, "gb2312")+"...";
            else
               returnnew String(b, 0, len - 4, "gb2312")+"...";
         }catch(Exception ex){      
        return"";
        }
    }
二、在fn.tld中加:
<function>
    <description>
      Removes white spaces from both ends of a string.
    </description>
    <name>getLimitStr</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String getLimitStr(java.lang.String,int)</function-signature>
    <example>
      Name: ${fn.getLimitStr(str,20)}
    </example> 
 </function>
 
三、把Functions.class fn.tld压回standard.jar中。(两个必须都同时这样做)
四、本例使用的是JSTL1.1
五、在编译JSTL源码时,需要在apache网站下载xalan-j_2_7_0.jar这个包。
 
用SWT实现系统托盘(改进版):【上一篇】
Oracle错误:ORA-01631:【下一篇】
【相关文章】
  • jstl标签注意问题
  • JSTL资料收集
  • 最简版(Hibernate+Struts+JSTL)环境架设
  • JSP标准标签库(JSTL)介绍
  • 一篇关于JSTL的好文章
  • 手把手教你在Eclipse+Tomcat环境下使用JSTL
  • 其实Struts和JSTL可以不在Web.xml里配置
  • Tomcat下构建JSTL(标准标签库)
  • JSTL(JSP标准标签库)介绍
  • struts表单 vs JSTL表现之多条件查询示例
  • 【随机文章】
  • VBScript While...Wend 语句
  • C++大师Stan Lippman:我对中国程序员的忠告
  • Visual SourceSafe 6.0 安装配置
  • Vmware下RedHat AS 2.1集群配置StepbyStep
  • 条款6:析构函数里对指针成员调用delete
  • 神奇克隆----Flash 残影效果
  • shell编程简介
  • 安装Linux应用软件的基本方法
  • Linux环境进程间通信(六):套接口
  • 未来的世界,桌面软件开发要失业了!
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.