zoukankan      html  css  js  c++  java
  • 自定义jstl标签*

    原文链接:https://www.it610.com/article/442039.htm

    步骤如下:


    1、写tld文档:用来指定标签的名字,标签库等。

    2、写标签处理器类。

    3、配置到web.xml中
    4、在jsp中使用新定义的标签

    例:实现一个自定义标签 功能如下 如果字符串长度超过规定长,则截取,并根据要求添加省略号

    tls文档:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"   
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
    <taglib>
        <tlib-version>1.0</tlib-version>
        <jsp-version>2.0</jsp-version>
        <short-name>ip</short-name>
        <uri>http://www.xx.tag</uri>
        <tag>
            <name>stringCut</name>
            <tag-class>com.xx.utils.jstl.JSTLStringCut</tag-class>
            <attribute>
                <name>str</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue><!-- 是否支持el表达式 -->
                <type>java.lang.String</type>
                <description>輸入字符串</description>
            </attribute>
            <attribute>
                <name>length</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
                <type>java.lang.Integer</type>
                <description>要显示字符串的长度</description>
            </attribute>
            <attribute>
                <name>showDot</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
                <type>java.lang.Boolean</type>
                <description>是否显示点号</description>
            </attribute>
        </tag>
    </taglib>

     标签处理器类:

    package com.xx.utils.jstl;
    
    import java.io.IOException;  
    
    import javax.servlet.jsp.JspException;  
    import javax.servlet.jsp.tagext.BodyTagSupport;  
    
    
    public class JSTLStringCut extends BodyTagSupport{  
          
        private String str ;  
        private Integer length ;
        private Boolean showDot; 
          
        @Override  
        public int doStartTag() throws JspException {  
          System.out.println(str);
          System.out.println(length);
          System.out.println(showDot);
            try {  
                if(str==null){
                    //do nothing
                }else{
                    if(str.length()>length){
                        str=str.substring(0,length);
                        if(showDot){
                            str=str+"...";
                        }
                    }
                    pageContext.getOut().print(str); 
                }
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return BodyTagSupport.EVAL_BODY_INCLUDE;//执行标签内容  
        }  
      
        @Override  
        public int doEndTag() throws JspException {  
            return BodyTagSupport.EVAL_BODY_INCLUDE;  
        }
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    
        public Integer getLength() {
            return length;
        }
    
        public void setLength(Integer length) {
            this.length = length;
        }
    
        public Boolean getShowDot() {
            return showDot;
        }
    
        public void setShowDot(Boolean showDot) {
            this.showDot = showDot;
        }  
      
    }  

    配置到web.xml

    <jsp-config>
             <taglib>        
                 <taglib-uri>http://www.xx.tag</taglib-uri>   
                 <taglib-location>/WEB-INF/tags/string-cut.tld</taglib-location>            
             </taglib>   
    </jsp-config>

     jsp中使用标签

    jsp页面顶部加入:

      

    <%@ taglib uri="http://www.xx.tag" prefix="ip" %>  
    <ip:stringCut str="${str}" length="10" showDot="true"></ip:stringCut>
  • 相关阅读:
    java获取程序执行时间
    自己不去努力 还有谁能帮你
    错误: 找不到或无法加载主类 的解决办法
    不要迷信红黑树 哈希是一切
    nancy的诊断2
    nancy中的诊断功能面板1
    ironpython 2.75 在c#中的使用
    sqlserver2008创建数据库 报 Cannot read property is filestream 此属性不可用于sql server 7.0 解决
    结巴net 分词 配置文件路径,在网站中的出现问题的解决
    akka 练手 原来第一次是原封不动的返回传出去的参数
  • 原文地址:https://www.cnblogs.com/isme-zjh/p/11883167.html
Copyright © 2011-2022 走看看