JSTL.带标签体的标签,方法和例子

1.

实现 forEach 标签: 两个属性: items(集合类型, Collection), var(String 类型)

doTag:
遍历 items 对应的集合
 把正在遍历的对象放入到 pageContext 中, 键: var, 值: 正在遍历的对象.
把标签体的内容直接输出到页面上.

(1)首先建立一个customer类:

package com.lanqiao.javatest;

public class Customer {
    private Integer id;
    private String name;
    private int age;
    public Customer() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Customer(Integer id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
    
}

(2)ForEachTag类,继承于父类: SimpleTagSupport

package com.atguigu.javaweb;

import java.io.IOException;
import java.util.Collection;

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

public class ForEachTag extends SimpleTagSupport{

    private Collection<?> items;
    
    public void setItems(Collection<?> items) {
        this.items = items;
    }
    
    private String var;
    
    public void setVar(String var) {
        this.var = var;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
//        * 遍历 items 对应的集合
        if(items != null){
            for(Object obj: items){
        //        * 把正在遍历的对象放入到 pageContext 中, 键: var, 值: 正在遍历的对象. 
                getJspContext().setAttribute(var, obj);
                
                //把标签体的内容直接输出到页面上. 
                getJspBody().invoke(null); 
                
            }
        }
        
    }
    
}

(3)在lib下建立一个mytag.tld的xml文件,

<?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">

    <description>MyTag 1.2 core library</description>
    <display-name>MyTag core</display-name>
    <tlib-version>1.2</tlib-version>
    <short-name>lxn</short-name>
    <uri>http://lxn.com/myTag/core</uri>

    
    
    <tag>
        <name>forEach</name>
        <tag-class>com.lanqiao.javatest.ForEachTag</tag-class>
        <body-content>scriptless</body-content>
        
        <attribute>
            <name>items</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

</taglib>

(4)建立一个test.jsp页面:

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.atguigu.javaweb.Customer"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="lxn" uri="http://lxn.com/myTag/core" %>
<!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> <br><br> <% List<Customer> customers = new ArrayList<Customer>(); customers.add(new Customer(1, "AAA")); customers.add(new Customer(2, "BBB")); customers.add(new Customer(3, "CCC")); customers.add(new Customer(4, "DDD")); customers.add(new Customer(5, "EEE")); request.setAttribute("customers", customers);
%> <lxn:forEach items="${requestScope.custoemrs }" var="cust"> -- ${cust.id } -- ${cust.name } <br> </lxn:forEach> <br><br> </body> </html>

2.使用标签处理器类,其中有一些方法:

package com.atguigu.javaweb;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//标签处理器类
public class TestJspFragment extends SimpleTagSupport {
    
    @Override
    public void doTag() throws JspException, IOException {

    //JspFragment,是标签体,getJspBody()是获取标签体 JspFragment bodyContent
= getJspBody();
//JspFragment.invoke(Witer): Writer 即为标签体内容输出的字符流, 若为 null, 则 //输出到 getJspContext().getOut(), 即输出到页面上. //1. 利用 StringWriter 得到标签体的内容. StringWriter sw = new StringWriter(); bodyContent.invoke(sw); //2. 把标签体的内容都变为大写 String content = sw.toString().toUpperCase(); //3. 获取 JSP 页面的 out 隐含对象, 输出到页面上 getJspContext().getOut().print(content); } }
原文地址:https://www.cnblogs.com/lxnlxn/p/5830928.html