javaweb 自定义标签

一:自定义foreach标签

1、foreach为集合性子的,所以先定义一个items集合,建一个ForEachTag.java

<span style="font-size:18px;">package com.hp;

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 {
		//1、遍历items集合	
		if(items != null){
			for(Object obj:items){
				//把正在遍历的对象放到pageContext中,键:var 值:正在遍历的对象	
				getJspContext().setAttribute(var, obj);
				//把标签体的内容直接输出到页面
				getJspBody().invoke(null);	
			}
			
		}
	}
	

}
</span>

同时建一个实体类:Customer.java

package com.hp;

public class Customer {

	private int id;
	private String  name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Customer(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public  Customer(){
		
	}
}

2、在lib下建一个Mytag.tld

<span style="font-size:18px;"><?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>athp</short-name>
	<uri>http://athp.com/MyTag/core</uri></span>
<span style="font-size:18px;"><pre name="code" class="java"><tag>
		<name>forEach</name>                          
		<tag-class>com.hp.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></span>


 
3、建一个测试界面test.jsp 

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%></span>
<span style="font-size:18px;"><%@page import="com.hp.Customer"%>
</span>
<span style="font-size:18px;"><pre name="code" class="java"><%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'test.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body></span>
<span style="font-size:18px;"><%
		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);</span>

%>

 
<span style="font-size:18px;"><athp:forEach items="${requestScope.customers }" var="customer">
     ${customer.id }   ${customer.name } <br>
<span style="white-space:pre">	</span></athp:forEach></span>
<span style="font-size:18px;"></body>
</html>

</span>

二:自定义父、子标签

ParentTag.java

package com.hp;

import java.io.IOException;

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

public class ParentTag extends SimpleTagSupport{
	
	private String name="ATHP";
	public String getName() {
		return name;
	}
	
	
	@Override
	public void doTag() throws JspException, IOException {
		System.out.println("父标签处理类name"+name);
		getJspBody().invoke(null);
	}

}
SonTag.java

<span style="font-size:18px;">package com.hp;

import java.io.IOException;

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

public class SonTag extends SimpleTagSupport{
	@Override
	public void doTag() throws JspException, IOException {
		//1、得到父标签的引用
		JspTag parent=getParent();
		//2、获取父标签的name属性
		ParentTag parentTag=(ParentTag) parent;
		String name=parentTag.getName();
		//3、把name值打印到JSP页面上
		getJspContext().getOut().print("子标签输出name:"+name);
	}

}
</span>

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.2 core library</description>
	<display-name>MyTag core</display-name>
	<tlib-version>1.2</tlib-version>
	<short-name>athp</short-name>
	<uri>http://athp.com/MyTag/core</uri>
<pre name="code" class="java"><tag>
		<name>parentTag</name>
		<tag-class>com.hp.ParentTag</tag-class>
		<body-content>scriptless</body-content>
	</tag>
	
	<tag>
		<name>sonTag</name>
		<tag-class>com.hp.SonTag</tag-class>
		<body-content>empty</body-content>
	</tag>

</taglib>

 test.jsp 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%>
<pre name="code" class="java"><%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'test.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>


 
<pre name="code" class="java">	<!-- 父标签打印name值到控制台 -->
	<athp:parentTag>
		<!-- 子标签以父标签的标签体存在,子标签把父标签的属性打印到页面 -->
		<athp:sonTag/>
	</athp:parentTag>

</body>
</html>
 

三:自定义选择标签

ChoseTag.java

<span style="font-size:18px;">package com.hp;

import java.io.IOException;

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

public class ChoseTag 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);	
	}

}
</span>
WhenTag.java

<span style="font-size:18px;">package com.hp;

import java.io.IOException;

import javax.servlet.jsp.JspException;
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 {
		if(test){
		ChoseTag choseTag=(ChoseTag) getParent();
		boolean flag=choseTag.isFlag();
		
		if(flag){
			getJspBody().invoke(null);
			choseTag.setFlag(false);
		}
		}
	}
	

}
</span>
OtherwiseTag.java

<span style="font-size:18px;">package com.hp;

import java.io.IOException;

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

public class OtherwiseTag extends SimpleTagSupport {
	@Override
	public void doTag() throws JspException, IOException {
		ChoseTag choseTag=(ChoseTag) getParent();
		
		if(choseTag.isFlag()){
			getJspBody().invoke(null);			
		}
	}
	
	

}
</span>
Mytag.tld

<span style="font-size:18px;"><?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>athp</short-name>
	<uri>http://athp.com/MyTag/core</uri></span>
<span style="font-size:18px;"><tag>
		<name>choseTag</name>
		<tag-class>com.hp.ChoseTag</tag-class>
		<body-content>scriptless</body-content>
	</tag>
	
	<tag>
		<name>whenTag</name>
		<tag-class>com.hp.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.hp.OtherwiseTag</tag-class>
		<body-content>scriptless</body-content>
	</tag></span>
<span style="font-size:18px;"></taglib>
</span>
test.jsp

<span style="font-size:18px;"><%@page import="com.hp.Customer"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'test.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>
</span>
<span style="font-size:18px;"><body></span>
<span style="font-size:18px;"><%
		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);
		
		Map<String, Customer> customerMap=new HashMap<String,Customer>();
		customerMap.put("a", customers.get(0));
		customerMap.put("b", customers.get(1));
		customerMap.put("c", customers.get(2));
		customerMap.put("d", customers.get(3));
		customerMap.put("e", customers.get(4));
		request.setAttribute("customerMap", customerMap);
	%>

	<!-- 遍历 <c:forEach items="${customerMap }" var="cust">
		${cust.key } ${cust.value.id } ${cust.value.name }<br>
	</c:forEach>
	<br><br>
	<c:forEach items="${requestScope.customers }" var="customer">
     	${customer.id }   ${customer.name } <br>
	</c:forEach>

	<br>
	<athp:forEach items="${requestScope.customers }" var="customer">
     ${customer.id }   ${customer.name } <br>
	</athp:forEach> --></span>
<span style="font-size:18px;"><br>
	<athp:choseTag>
		<athp:whenTag test="${param.age >24}">大学毕业</athp:whenTag>
		<athp:whenTag test="${param.age >20}">高中毕业</athp:whenTag>
		<athp:otherWise>高中以下</athp:otherWise>
	</athp:choseTag></span>
<span style="font-size:18px;"></body>
</html></span>









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值