• 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); } }
  • 相关阅读:
    堆栈,堆栈,堆和栈的区别(转贴)
    .net cookie跨域和逗号bug的修复
    转:互联网协议入门
    函数式编程stream.js
    用例子验证w3c的stacklevel在不同浏览器中的显示
    JS创建对象的几种方法
    打开Word时总是出现 “Microsoft Office Word 需要 VBA 宏语言支持来完成此操作
    如何解决闭包只能取得包含函数中任何变量的最后一个值
    转:10个javascript简写/优化技巧
    Nicholas C. Zakas如何面试前端工程师
  • 原文地址:https://www.cnblogs.com/lxnlxn/p/5830928.html
Copyright © 2020-2023  润新知