java 自定义标签

1.自定义标签实现对遍历功能

ForEachTag.java

package com.geeksun.tag;

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;
	private String var;
	
	public void setItems(Collection<?> items)
	{
		this.items = items;
	}
	public void setVar(String var)
	{
		this.var = var;
	}
	@Override
	public void doTag() throws JspException, IOException
	{
		if(items != null)
		{
			for(Object obj:items)
			{
				getJspContext().setAttribute(var, obj);
				getJspBody().invoke(null);
			}
		}
	}
}

index.jsp

<%@page import="com.geeksun.one.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		//模拟servlet操作
		List<Customer>  customers = new ArrayList<Customer>();
		customers.add(new Customer("1","1","1","1"));
		customers.add(new Customer("2","2","2","2"));
		customers.add(new Customer("3","3","3","3"));
		customers.add(new Customer("4","4","4","4"));
		customers.add(new Customer("5","5","5","5"));
		request.setAttribute("customers", customers);
	%>
	<Geeksun:forEach items="${requestScope.customers }" var="customer">
 	${customer.name },${customer.address },${customer.cardType },${customer.card }
	<br><br> 
 	</Geeksun:forEach>
</body>
</html>

2.利用带父标签的标签实现if else if else的功能
ChooseTag.java

package com.geeksun.tag2;

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

public class ChooseTag extends SimpleTagSupport
{
	private boolean flag = true;
	public void setFlag(boolean flag)
	{
		this.flag = flag;
	} 
	public boolean isFlag()
	{
		return flag;
	}
	@Override
	public void doTag() throws JspException, IOException
	{
		getJspBody().invoke(null);
	}
}

WhenTag.java

package com.geeksun.tag2;

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

public class WhenTag extends SimpleTagSupport
{
	private boolean test;
	public void setTest(boolean test)
	{
		this.test = test;
	}
	@Override
	public void doTag() throws JspException, IOException
	{
		JspTag jspTag = getParent();
		ChooseTag parentTag = (ChooseTag)jspTag;
		if(parentTag.isFlag() == true&&test == true)
		{
			parentTag.setFlag(false);
			getJspBody().invoke(null);
		}
	}
}

OtherwiseTag.java

package com.geeksun.tag2;

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

public class OtherwiseTag extends SimpleTagSupport
{
	private boolean test;
	public void setTest(boolean test)
	{
		this.test = test;
	}
	@Override
	public void doTag() throws JspException, IOException
	{
		JspTag jspTag = getParent();
		ChooseTag parentTag = (ChooseTag)jspTag;
		if(parentTag.isFlag() == true)
		{
			getJspBody().invoke(null);
		}
	}
}

TestTag.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.Geeksun.com/mytag/core/" prefix="Geeksun"%>
<!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>
    <Geeksun:Choose>
        <Geeksun:When test="${param.age > 22}">大学毕业</Geeksun:When>
        <Geeksun:When test="${param.age > 20}">高中毕业</Geeksun:When>
        <Geeksun:Otherwise>高中未毕业</Geeksun:Otherwise>
    </Geeksun:Choose>
</body>
</html>

mytag.tld

<?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.1 core library</description>
	<display-name>MyTag core</display-name>
	<tlib-version>1.0</tlib-version>    
    <!--定义一个简短的名称,主要是给一些工具使用(必须元素)-->  
	<short-name>Geeksun</short-name>
    <!--定义此标签库的uri路径,用于唯一标识该数据库,便于页面的引用-->
	<uri>http://www.Geeksun.com/mytag/core/</uri>
    <!--此标签库中的一个标签处理器的声明--> 
	<tag>
        <!--该标签名称(必须元素)-->  
		<name>Choose</name>
        <!--该标签处理类的全限定名(必须元素)-->
		<tag-class>com.geeksun.tag2.ChooseTag</tag-class>
        <!--指明该标签主题类型)-->  
		<body-content>scriptless</body-content>
	</tag>
	<tag>
		<name>When</name>
		<tag-class>com.geeksun.tag2.WhenTag</tag-class>
		<body-content>scriptless</body-content>
		<attribute>
			<name>test</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
		<tag>
		<name>Otherwise</name>
		<tag-class>com.geeksun.tag2.OtherwiseTag</tag-class>
		<body-content>scriptless</body-content>
	</tag>
	<tag>
		<name>forEach</name>
		<tag-class>com.geeksun.tag.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>
  • <description><display-name>:作用与taglib的一样,可以有多个。
  • <name>:必选,标签名称。
  • <tag-class>:标签处理类。
  • <tei-class>:可选,javax.servlet.jsp.tagext.TagExtraInfo的继承类,用于校验tag的属性,确保使用的正确,<c:import><c:forEach>就有定义。
  • <body-content>:必选,有四个选项empty、scriptless、JSP、tagdependent,之后篇章介绍。
  • <variable>:可选,可定义多个,JSTL TLD中都没有使用,它是用来提供tag的结果的各种变量信息。什么是variable?我们通过pageContext.setAttribute/getAttribute设置或获取某个值,这就是变量。不通过variable,我们也可以通过EL来获取。如果提供了variable,我们除了EL外,还可以在java代码中使用这些变量。但现在都是采用MVC模式,JSP页面中java代码基本上也不会被使用。子参数依次包括:

    • <description>
    • <name-given> 变量的名字 。
    • <name-from-attribute> 决定变量名字,这和<name-given>相互冲突,只能二选一。String,不能是EL等在运行时计算 。
    • <variable-class> 变量的类型 。
    • <declare> true表示变量是新,需要声明;false表示已存在,只是修改值。
    • <scope> 缺省是NESTED,即在tag内有效;AT_BEGIN表示变量tag内及之后有效;AT_END表示变量只在tag后有效。
  • <attribute>:可选,可定义多个,定义Tag的属性。

    • <description>:可选。 <name>:属性名称。
    • <required:可选,属性是否必须true/false,缺省为false。
    • <rtexprvalue:可选,rtexprvalue是runtime expression value:true表示允许EL这类运行时计算值,false则不允许,缺省为false。
    • <type>:可选,属性类型,缺省为Object。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值