JSTL,自定义一个标签的功能案例

1.自定义一个带有两个属性的标签<max>,用于计算并输出两个数的最大值;

2.自定义一个带有一个属性的标签<lxn:readFile  src=“”>,用于输出指定文件的内容;

------------------------------------------------------------------------------------

首先在src目录下建立一个类:MaxTag,其继承于父类:SimpleTagSupport,里边可实现两个数比较的方法

package com.lanqiao.javatest;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MaxTag  extends SimpleTagSupport{
    private String num1;
    private String num2;
    
    public void setNum1(String num1) {
        this.num1 = num1;
    }
    
    public void setNum2(String num2) {
        this.num2 = num2;
    }
    
    public void doTag() throws  JspException, IOException{
        int a;
        int b;
        
        PageContext pageContext=(PageContext)getJspContext();
        
        JspWriter out=pageContext.getOut();
        try {
            a=Integer.parseInt(num1);
            b=Integer.parseInt(num2);
            out.print(a > b ? a:b);
        } catch (Exception e) {
            out.print("输入的格式不正确!!!");
        }
    }
    
}

在src目录下建立一个类:ReadFileTag,其继承于父类:SimpleTagSupport,使用了正则表达式,对文件中带有的标签进行转化,实现文件在页面上显示;

package com.lanqiao.javatest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ReadFileTag extends SimpleTagSupport{

    //相对于当前 WEB 应用的根路径的文件名
    private String src;

    public void setSrc(String src) {
        this.src = src;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) getJspContext();
        InputStream in = pageContext.getServletContext().getResourceAsStream(src);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
        
        String str = null;
        while((str = reader.readLine()) != null){
            //对文件中带标签的<>进行转化:使用了正则表达式
            str = Pattern.compile("<").matcher(str).replaceAll("&lt");
            str = Pattern.compile(">").matcher(str).replaceAll("&gt");
            
            pageContext.getOut().println(str);
            pageContext.getOut().println("<br>"); 
        }
    }
    
}

在lib下,建立一个mytag.tld的xml文件,里面有一些配置,将jsp和class的数据进行连接;

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <!-- 描述 TLD 文件 -->
    <description>MyTag 1.0 core library</description>
    <display-name>MyTag core</display-name>
    <tlib-version>1.0</tlib-version>

    <!-- 建议在 JSP 页面上使用的标签的前缀 -->
    <short-name>lxn</short-name>
    <!-- 作为 tld 文件的 id, 用来唯一标识当前的 TLD 文件, 多个 tld 文件的 URI 不能重复. 通过 JSP 页面的 taglib 
        标签的 uri 属性来引用. -->
    <uri>http://www.lxn.com/mytag/core</uri>
    
    <!-- 描述自定义的 ReadFileTag 标签 -->
    <tag>
        <!-- 标签的名字: 在 JSP 页面上使用标签时的名字 -->
        <name>readerFile</name>
        
        <!-- 标签所在的全类名 -->
        <tag-class>com.lanqiao.javatest.ReadFileTag</tag-class>
        
        <!-- 标签体的类型 -->
        <body-content>empty</body-content>
        
        <!-- 描述当前标签的属性 -->
        <attribute>
            <!-- 属性名 -->
            <name>src</name>
            
            <!-- 该属性是否被必须 写-->
            <required>true</required>
            
            <!-- rtexprvalue: runtime expression value 当前属性是否可以接受运行时表达式的动态值 -->
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    
    <tag>
        <name>max</name>
        <tag-class>com.lanqiao.javatest.MaxTag</tag-class>
        <body-content>empty</body-content>
    
        <attribute>
            <name>num1</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>num2</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    

</taglib>  

最后建立一个jsp页面;在WEB-INF目录下建立一个note.file的file文件,实现读取的功能;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<!-- 导入标签库(描述文件) -->
<%@ taglib uri="http://www.lxn.com/mytag/core" prefix="lxn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <lxn:readerFile src="/WEB-INF/note.file"/>
    
    <lxn:max num2="${param.a }" num1="${param.b }"/>
    
</body>
</html>
原文地址:https://www.cnblogs.com/lxnlxn/p/5829378.html