码迷,mamicode.com
首页 > Web开发 > 详细

自定义jsp标签

时间:2019-06-16 23:16:17      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:clu   ansi   再计算   odi   eval   com   img   自定义标签库   style   


1、标签库的运作离不开tld文件
2、标签库的标签是定义在tld中的tag标签内(助手类)

标签的语言特点

  <开始标签 属性="属性值">标签体</结束标签>


3. 自定义标签的开发及使用步骤
3.1 创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法

3.2 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
3.3 在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签

4.标签生命周期

技术图片


SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次

 

作业:

foreach标签的助手类:

package com.tag;

import java.util.List;

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

public class ForeachTag extends BodyTagSupport{

    /**
     * 
     */
    private static final long serialVersionUID = -961767522031795797L;

    private List<Object> itmes;
    private String var;
    
    public ForeachTag(List<Object> itmes, String var) {
        super();
        this.itmes = itmes;
        this.var = var;
    }
    
    public ForeachTag() {
        super();
        // TODO Auto-generated constructor stub
    }

    public List<Object> getItmes() {
        return itmes;
    }
    public void setItmes(List<Object> itmes) {
        this.itmes = itmes;
    }
    public String getVar() {
        return var;
    }
    public void setVar(String var) {
        this.var = var;
    }
    @Override
    public int doStartTag() throws JspException {
        // TODO Auto-generated method stub
        if (itmes==null) {
            return SKIP_BODY;
        }
        return EVAL_BODY_INCLUDE;
    }
}

if标签的助手类:

package com.tag;

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

public class IfTag extends BodyTagSupport {


    /**
     * 
     */
    private static final long serialVersionUID = -5147827787787703458L;

    private boolean test;
    
    public boolean isTest() {
        return test;
    }
    public void setTest(boolean test) {
        this.test = test;
    }
    @Override
    public int doStartTag() throws JspException {
        if (!test) {
            return SKIP_BODY;//不显示标签中间的内容
        }
        return EVAL_BODY_INCLUDE;//显示标签中的内容
    }
}

out标签的助手类:

package com.tag;

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

public class OutTag extends BodyTagSupport{

    /**
     * 
     */
    private static final long serialVersionUID = -2971786012488394710L;
    
    private Object value;

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            out.write(value.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        }
        return SKIP_BODY;
    }
}

set标签的助手类:

package com.tag;

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

public class SetTag extends BodyTagSupport{
       
    /**
     * set标签
     */
    private static final long serialVersionUID = -5972588683307720977L;
    private Object value;
    
      public Object getValue() {
        return value;
    }

    public SetTag(Object value) {
        super();
        this.value = value;
    }

    public void setValue(java.lang.Object value) {
          this.value=value;
    }
        
      public void setTarget(java.lang.Object target) {
          this.value=target;
    }
     @Override
    public int doStartTag() throws JspException {
         JspWriter out = pageContext.getOut();
            try {
                out.write(value.toString());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                throw new RuntimeException();
            }
            return SKIP_BODY;
    }
      
}

自定义标签库:

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

<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescrtiptor">

    <tlib-version>1.1</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>Simple Tags</short-name>
    <uri>/lingerqi</uri>


  <tag>
    <name>out</name>
    <tag-class>com.tag.OutTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  
  <tag>
    <name>Foreach</name>
    <tag-class>com.tag.ForeachTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
   
  <tag>
    <name>if</name>
    <tag-class>com.tag.IfTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  <tag>
      <name>set</name>
      <tag-class>com.tag.SetTag</tag-class>
      <body-content>JSP</body-content>
      <attribute>
          <name>value</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue>
      </attribute>
  </tag>
</taglib>

调用标签:

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="z" uri="/lingerqi" %>
<!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>
<%--  <%
        List<String> ls = new ArrayList<>();
        ls.add("zc");
        ls.add("ls");
        ls.add("ww");
        ls.add("zl");
        ls.add("so");
        ls.add("wl");
        pageContext.setAttribute("ls", ls);
    %>
    <ol>
        <l:foreach var="l" itmes="${ls }">
    <li>${ls}</li>
        </l:foreach>
    </ol>  --%>
    <%-- <z:out></z:out> --%>
    <%-- <z:if>{1>1}</z:if> --%>
</body>
</html>

 

自定义jsp标签

标签:clu   ansi   再计算   odi   eval   com   img   自定义标签库   style   

原文地址:https://www.cnblogs.com/omji0030/p/11037306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!