此例子标签的效果
开发步骤
定义实体类
public class Emp {
private String empId;
private String name;
private String phoneNumber;
public Emp() {
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
创建标签库描述文件:/WEB-INF/tag/emptag.tdl
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>webapp</short-name>
<uri>/webapptaglib</uri>
<tag>
<name>empInfo</name>
<tag-class>com.sdTag.EmpTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>emp</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在web.xml中配置映射
<jsp-config>
<taglib>
<taglib-uri>/webapptaglib</taglib-uri>
<taglib-location>/WEB-INF/tag/emptag.tdl</taglib-location>
</taglib>
</jsp-config>
自定义标签类
package com.sdTag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.Emp;
public class EmpTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private Emp emp;
public int doStartTag() throws JspException {
JspWriter out = this.pageContext.getOut();
try {
if (emp == null) {
out.println("No data Found...");
return SKIP_BODY;
}
out.println("<table width='500px' border='3' bordercolor='red' align='center'>");
out.println("<tr>");
out.println("<td width='20%'>Emp Id:</td>");
out.println("<td>" + emp.getEmpId() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Name:</td>");
out.println("<td>" + emp.getName() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Phone Number:</td>");
out.println("<td>" + emp.getPhoneNumber() + "</td>");
out.println("</tr>");
out.println("</table>");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
@Override
public void release() {
super.release();
this.emp = null;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
}
最后在JSP中引用自定义标签
<%@page import="com.Emp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/webapptaglib" prefix="webapp"%>
<!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>
<%
Emp emp = new Emp();
emp.setEmpId("AP001");
emp.setName("Tom");
emp.setPhoneNumber("10086");
pageContext.setAttribute("emp", emp);
%>
<webapp:empInfo emp="${pageScope.emp }" />
</body>
</html>