返回顶部

自定义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>

 

posted @ 2019-11-18 16:44  fen斗  阅读(134)  评论(0编辑  收藏  举报