jsp自定义标签实例介绍,jsp自定义标签怎么用?
Parent_tag.java源码实例:
package com.cn.customtag;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class Parent_tag extends BodyTagSupport {
private List<Map<String, String>> columns = new ArrayList<Map<String, String>>();
private Object items; // 声明一个Object对象,用于存储数据
private String url; // 声明一个String型变量,用于存储路径
//覆盖doStartTag方法, 标签开始时执行
public int doStartTag() throws JspException{
columns.clear(); // 清空columns 变量
return super.doStartTag();
}
//覆盖doAfterBody方法,标签体结束后执行
@SuppressWarnings(“unchecked”)
public int doAfterBody() throws JspException{
try{
BodyContent bc = getBodyContent(); // 获取标签体,赋值给BodyContent对象
JspWriter out = bc.getEnclosingWriter(); // 使用bc对象的方法获取out对象
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
String orderName = request.getParameter(“orderName”); // 获得排列顺序名称,
String orderType = request.getParameter(“orderType”); // 获得排序方式
orderType = “desc”.equals(orderType) ? “desc” : “asc”; // 使用三目运算判断是升序还是降序
out.println(“<table id=theObjTable”); // 在页面输出<table>标签
out.println(” STYLE=’table-layout:fixed;’ >”);
out.println(” <tr>”); // 在页面输出<tr>标签
out.println(” <script>var columns = []; </script>”);
for (int i = 0; i < columns.size(); i++){
Map<String, String> column = columns.get(i); // 获取Map集合中的列数据
String label = column.get(“label”); // 使用Map对象获得列的名称
String property = column.get(“property”); // 使用Map对象获得列对应的POJO属性
label = label == null ? property : label; // 默认使用属性作为列名
out.println(“<td id=’__id_td_” + property + “‘>”); // 在页面输出<td>标签
out.println(“<font class=’resizeDivClass’”); // 在页面输出<font>标签
out.println(” onmousedown=’MouseDownToResize(this);’);”);
out.println(” onmousemove=’MouseMoveToResize(this);’”);
out.println(” onmouseup=’MouseUpToResize(this);’></font>”);
out.println(“<span onclick=\”sort(‘” + property + “‘); \”");
out.println(” style=\”cursor: pointer; \”>”);
out.println(label); // 在页面输出列名称
if (property.equals(orderName)){
out.println(“<img src=’images/” + orderType + “.gif’ border=0/>”); // 在页面输出排序图标
}
out.println(“</span>”);
out.println(“</td>”);
out.println(“<script>columns[columns.length] = ‘__id_td_” + property + “‘; </script>”);
}
out.println(” </tr>”);
if (items != null){
for (Object obj : (Iterable) items) {
out.println(” <tr>”);
for (int i = 0; i < columns.size(); i++){
Map<String, String> column = columns.get(i);
String property = column.get(“property”); // 对象属性名
String getterStyle = toGetterStyle(property); // 使首字母大写的方法
try{
String getter = “get” + getterStyle;
String is = “is” + getterStyle;
Method method = null;
try{
method = obj.getClass().getMethod(getter);
}catch (Exception e){}
if (method == null){
method = obj.getClass().getMethod(is);
}
method.setAccessible(true); // 使getter方法可见,括号中为false表示不可见
Object value = method.invoke(obj); // 得到getter方法返回值
out.println(“<td><span title=’” + value + “‘>” + value + “</span></td>”);// 在页面输出数据
}catch (Exception e) {
throw new JspException(e);
}
}
out.println(” </tr>”);
}
}
out.println(“</table>”);
}
catch (IOException ioe){
throw new JspException(“Error: ” + ioe.getMessage());
}
return SKIP_BODY;
}
public String toGetterStyle(String column) { // 使字符串的首字母大写
if (column.length() == 1)
return column.toUpperCase();
char ch = column.charAt(0);
return Character.toUpperCase(ch) + column.substring(1);
}
public List<Map<String, String>> getColumns() {
return columns;
}
public void setColumns(List<Map<String, String>> columns) {
this.columns = columns;
}
public Object getItems() {
return items;
}
public void setItems(Object items) {
this.items = items;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Son_tag.java源码:
package com.cn.customtag;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Son_tag extends TagSupport {
private String property; // 声明一个变量,表示列对应的对象属性名
private String label; // 声明一个变量,表示列名
// 覆盖doStartTag方法,标签开始时执行
public int doStartTag() throws JspException{
if (!(this.getParent() instanceof Parent_tag)){ // 判断子标签如果不在Table标签内
throw new JspException(“Column must be inside Table. “); // 则抛出异常
}
Map<String, String> column = new HashMap<String, String>();
column.put(“label”, label); // 把列名保存到column对象中
column.put(“property”, property); // 把对象属性名保存到column对象中
Parent_tag table = (Parent_tag) this.getParent(); // 获取父标签
table.getColumns().add(column); // 添加该列
return SKIP_BODY;
}
// 覆盖doEndTag方法,标签结束时执行
public int doEndTag() throws JspException{
return EVAL_PAGE;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
taglib.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 web-jsptaglibrary_2_0.xsd”
version=”2.0″>
<!– 自定义一个标签库 –>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>tags</short-name>
<uri>custom.tag</uri>
<description>JSP custom tags</description>
<tag>
<name>table</name>
<tag-class>com.cn.customtag.Parent_tag</tag-class>
<body-content>JSP</body-content>
<info>Table tag.</info>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>column</name>
<tag-class>com.cn.customtag.Son_tag</tag-class>
<body-content>empty</body-content>
<info>Column tag.</info>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>label</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
nest_tags.jsp源码:
<%@ page language=”java” contentType=”text/html; charset=utf-8″%>
<%@ taglib uri=”custom.tag” prefix=”tags”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘nest_tags.jsp’ starting page</title>
</head>
<body>
<tags:table items=”${ personList }” url=”table.jsp” >
<tags:column property=”id” label=”编号” />
<tags:column property=”name” label=”姓名” />
<tags:column property=”age” label=”年龄” />
<tags:column property=”sex” label=”性别” />
<tags:column property=”address” label=”地址” />
<tags:column property=”telephone” label=”电话” />
<tags:column property=”mobile” label=”手机” />
<tags:column property=”city” label=”城市” />
<tags:column property=”deleted” label=”是否删除” />
</tags:table>
<tags:table items=”${ personList }” url=”table.jsp”>
<tags:column property=”id” label=”001″ />
<tags:column property=”name” label=”李四” />
<tags:column property=”age” label=”22″ />
<tags:column property=”sex” label=”男” />
<tags:column property=”address” label=”北京” />
<tags:column property=”telephone” label=”010-12345678″ />
<tags:column property=”mobile” label=”138********” />
<tags:column property=”city” label=”北京” />
<tags:column property=”deleted” label=”否” />
</tags:table>
<tags:table items=”${ personList }” url=”table.jsp”>
<tags:column property=”id” label=”002″ />
<tags:column property=”name” label=”张三” />
<tags:column property=”age” label=”22″ />
<tags:column property=”sex” label=”男” />
<tags:column property=”address” label=”北京” />
<tags:column property=”telephone” label=”010-12343278″ />
<tags:column property=”mobile” label=”138********” />
<tags:column property=”city” label=”北京” />
<tags:column property=”deleted” label=”否” />
</tags:table>
</body>
</html>
web.xml文件:
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<jsp-config>
<taglib>
<taglib-uri>custom.tag</taglib-uri>
<taglib-location>taglib.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>