JavaWeb----自定义标签的开发

最大值标签:

MaxNumberTag.java
package cn.limbo.tags;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
import java.io.IOException;

/**
 * Created by Limbo on 2016/9/21.
 */
public class MaxNumberTag implements SimpleTag{

    private String num1;
    private String num2;
    private PageContext pageContext;

    public void setNum1(String num1) {

        this.num1 = num1;
    }

    public void setNum2(String num2) {

        this.num2 = num2;
    }

    @Override
    public void doTag() throws JspException, IOException {
        Integer num1Integer = Integer.parseInt(num1);
        Integer num2Integer = Integer.parseInt(num2);
        Integer max = num1Integer > num2Integer ? num1Integer : num2Integer;
        this.pageContext.getOut().print("最大的数是: " + max);
    }

    @Override
    public void setParent(JspTag jspTag) {

    }

    @Override
    public JspTag getParent() {

        return null;
    }

    @Override
    public void setJspContext(JspContext jspContext) {
        this.pageContext = (PageContext) jspContext;
    }

    @Override
    public void setJspBody(JspFragment jspFragment) {

    }
}
myTag.tld
 <tag>
        <name>max</name>
        <tag-class>cn.limbo.tags.MaxNumberTag</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>


转换大写标签:

ToUpperCaseTag.java
package cn.limbo.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;

/**
 * Created by Limbo on 2016/9/21.
 */
public class ToUpperCaseTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        //获取标签体中的内容
        StringWriter sw = new StringWriter();
        getJspBody().invoke(sw);  //将标签体中的内容以输出流写入到sw中,如果为null直接输出到屏幕
        String content = sw.toString().toUpperCase();//转大写
        //输出到页面
        getJspContext().getOut().print(content);

    }
}
myTag.tld
<tag>
        <name>uppercase</name>
        <tag-class>cn.limbo.tags.ToUpperCaseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

迭代标签:

ForEachTag.java
package cn.limbo.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Limbo on 2016/9/21.
 */
public class ForEachTag extends SimpleTagSupport{

    private Collection<Object> collection;
    private String var;
    private Object items;

    public void setItems(Object items) {

        this.items = items;
        //c标签的源码也是这样写的 利用一堆的instanceof来判断传入参数的类型
        if(items instanceof Collection)
        {
            collection = (Collection<Object>) items;
        }
        if(items instanceof Map)
        {
            Map map = (Map) items;
            collection = map.entrySet();
        }
        //加上这个就可以使用8种基本类型的迭代
        if(items.getClass().isArray()) //这个是最重要的,反射判断
        {
            this.collection = new ArrayList<>();
            int length = Array.getLength(items);
            for(int i = 0; i < length; ++i)
            {
                Object value = Array.get(items,i);
                collection.add(value);
            }
        }
    }

    public void setVar(String var) {

        this.var = var;
    }

    @Override
    public void doTag() throws JspException, IOException {
    //遍历items对应的集合
        Iterator it = this.collection.iterator();
        while(it.hasNext())
        {
            Object value = it.next();
            //把正在遍历的对象放到pageContext中 键:var 值:正在遍历的对象
            this.getJspContext().setAttribute(var,value);
            //把标签体的内容输出到页面上
            this.getJspBody().invoke(null);
        }




    }
}
myTag.tld
<tag>
        <name>forEachTag</name>
        <tag-class>cn.limbo.tags.ForEachTag</tag-class>
        <body-content>JSP</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>
应用:

<%@ page import="cn.limbo.tags.Customer" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%--
  Created by IntelliJ IDEA.
  User: Limbo
  Date: 2016/9/21
  Time: 13:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="limbo" uri="http://www.limbo.com/jsp/jstl/core" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <%
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer(1,"AA"));
        customers.add(new Customer(2,"BB"));
        customers.add(new Customer(3,"CC"));
        customers.add(new Customer(4,"DD"));
        customers.add(new Customer(5,"EE"));
        Map<String , List<Customer> > map = new HashMap<>();
        map.put("customers",customers);
        request.setAttribute("map",map);

    %>

    <limbo:hello></limbo:hello>
    <limbo:max num1="12" num2="17"></limbo:max>
    <br><br>
    <limbo:forEachTag items="${requestScope.map}" var="cust" >
       <limbo:forEachTag items="${cust.value}" var="content">
           ID: ${content.id} ---- Name: ${content.name}<br><br>
       </limbo:forEachTag>
    </limbo:forEachTag>
    <limbo:uppercase>hello world</limbo:uppercase>
    <%--<c:forEach items="${requestScope.map}" var="cust">--%>
        <%--${cust.key} ---- ${cust.value}--%>
    <%--</c:forEach>--%>
</body>
</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值