JavaEE 自定义标签

自定义标签步骤

  • 定义处理类
  • 编写tld文件
  • 页面引用

自定义标签常量

skip body 跳到doEndTag
eval body include 执行标签体
eval body again 重复执行doAfterTag
skip page doEndTag release - 停止
eval page doEndTag release - 继续执行

示例

TagSupport:输出内容到页面

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class WriterTag extends TagSupport {

    private static final long serialVersionUID = 1L;

    @Override
    public int doStartTag() throws JspException {
        try {
            JspWriter jspOut = pageContext.getOut();
            jspOut.print("JSP页面内置的out就是JspWriter      JspWriter缓冲了再输出    print参数各种类型
"); jspOut.write("JSP页面内置的out就是JspWriter JspWriter缓冲了再输出 write参数String/char/int

"); // jspOut.flush();//刷新缓冲区保证先输出 PrintWriter servletOut = pageContext.getResponse().getWriter(); servletOut.print("Servlet里常用的out PrintWriter没有使用缓冲区所以先输出 print参数各种类型
"); servletOut.write("Servlet里常用的out PrintWriter没有使用缓冲区所以先输出 write参数String/char/int

"); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } }

TagSupport:转换日期

import java.text.SimpleDateFormat;
import java.util.Date;

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

public class DateFormat extends TagSupport {

    private static final long serialVersionUID = 1L;

    private String pattern;
    private String var;
    private Date date;    //可选 默认当前时间
    private String scope;  //可选 默认page作用域

    @Override
    public int doStartTag() throws JspException {
        if (date == null) {date = new Date();}

        SimpleDateFormat  sdf = new SimpleDateFormat(pattern);
        if (scope == null || "".equals(scope) || "page".equals(scope)) {
            pageContext.setAttribute(var, sdf.format(date),PageContext.PAGE_SCOPE);
        }else if ("request".equals(scope)) {
            pageContext.setAttribute(var, sdf.format(date),PageContext.REQUEST_SCOPE);
        }else if ("session".equals(scope)) {
            pageContext.setAttribute(var, sdf.format(date),PageContext.SESSION_SCOPE);
        }else if ("application".equals(scope)) {
            pageContext.setAttribute(var, sdf.format(date),PageContext.APPLICATION_SCOPE);
        }
        return SKIP_BODY;
    }

    //属性名随意 但是方法名必须与标签属性的名字有关系 保证能找到对应的方法
    public String getScope() {
        return scope;
    }
    public void setScope(String scope) {
        this.scope = scope;
    }
    public String getVar() {
        return var;
    }
    public void setVar(String var) {
        this.var = var;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getPattern() {
        return pattern;
    }
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
}

SimpleTagSupport

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

public class SimpleTagIterator extends SimpleTagSupport {
    private List items;
    private String var;
    private Iterator iterator;
    @Override
    public void doTag() throws JspException, IOException {
        iterator = items.iterator();
        while (iterator.hasNext()) {
            getJspContext().setAttribute(var, iterator.next());
            getJspBody().invoke(null);
        }
    }

    public List getItems() {
        return items;
    }
    public void setItems(List items) {
        this.items = items;
    }
    public String getVar() {
        return var;
    }
    public void setVar(String var) {
        this.var = var;
    }
}

编写mytags.tld




    1.0
    1.2
    baozi
    
        writer
        org.baozi.tag.WriterTag
        empty
    


    
        format
        org.baozi.tag.DateFormat
        empty
        
            pattern
            true
            true
        
        
            var
            true
            true
        
        
            date
            false
            true
        
        
            scope
            false
            true
        
    


    
        iter
        org.baozi.tag.IteratorTag
        JSP
        
            items
            true
            true
        
        
            var
            true
            true
        
    


    
        simpleiter
        org.baozi.tag.SimpleTagIterator
        scriptless
        
            items
            true
            true
        
        
            var
            true
            true
        
    

web.xml




    

    
        index.jsp
    

页面中引用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="baozi" uri="/WEB-INF/mytags.tld" %>
<%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        %>
        


    
    My JSP 'index.jsp' starting page



    <%
    List list = new ArrayList();
    list.add("AA");
    list.add("BB");
    list.add("CC");
    pageContext.setAttribute("list", list);
    %>
    
        ${test}-
    


你可能感兴趣的:(JavaEE 自定义标签)