一:自定义foreach标签
1、foreach为集合性子的,所以先定义一个items集合,建一个ForEachTag.java
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);
}
}
}
}
同时建一个实体类: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
MyTag 1.2 core library
MyTag core
1.2
athp
http://athp.com/MyTag/core
forEach
com.hp.ForEachTag
scriptless
items
true
true
var
true
true
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%>
<%@page import="com.hp.Customer"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'test.jsp' starting page
<%
List customers = new ArrayList();
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);
${customer.id } ${customer.name }
二:自定义父、子标签
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
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);
}
}
MyTag 1.2 core library
MyTag core
1.2
athp
http://athp.com/MyTag/core
parentTag
com.hp.ParentTag
scriptless
sonTag
com.hp.SonTag
empty
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'test.jsp' starting page
三:自定义选择标签
ChoseTag.java
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);
}
}
WhenTag.java
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);
}
}
}
}
OtherwiseTag.java
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);
}
}
}
Mytag.tld
MyTag 1.2 core library
MyTag core
1.2
athp
http://athp.com/MyTag/core
choseTag
com.hp.ChoseTag
scriptless
whenTag
com.hp.WhenTag
scriptless
test
true
true
otherWise
com.hp.OtherwiseTag
scriptless
test.jsp
<%@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 + "/";
%>
My JSP 'test.jsp' starting page
<%
List customers = new ArrayList();
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 customerMap=new HashMap();
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);
%>
大学毕业
高中毕业
高中以下