论坛首页 入门技术论坛

jsp 自定义标签简单应用

浏览 1249 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-04   最后修改:2010-01-04
后台类:

package ns.taglib;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

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

public class arrayListShowTab extends SimpleTagSupport {
// 要显示的arraylist数组
private ArrayList arr;
// arraylist 中对应map要显示的字段名称
private Map map;

public Map getMap() {
return map;
}

public void setMap(Map map) {
this.map = map;
}

public void doTag()throws JspException,IOException
{
try{
Writer out = getJspContext().getOut();
String tabString = "<table border='1' id='tagTab'>";
String td="<tr>";
ArrayList keyList = new ArrayList();
ArrayList valList = new ArrayList();
for(Iterator i = map.keySet().iterator();i.hasNext();){ 
    Object key = i.next(); 
    keyList.add(key.toString());
    Object val = map.get(key); 
    td +="<td>"+val.toString()+"</td>";
    valList.add(val.toString());
}
td += "</tr>";
tabString += td;
for(int i=0;i<arr.size();i++){
String tdInfo ="";
Map mp = (Map) arr.get(i);
for(int t=0;t<keyList.size();t++){
tdInfo += "<td>"+mp.get(keyList.get(t).toString()).toString()+"</td>";
}
tabString += "<tr>"+tdInfo+"</tr>";
}
out.write(tabString);
}catch(Exception e){
e.printStackTrace();
}
}
public ArrayList getArr() {
return arr;
}

public void setArr(ArrayList arr) {
this.arr = arr;
}


}


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">
      <description>arrTab Demo</description>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>TagDemo</short-name>
    <uri>/arrTab</uri>
    <tag>
    <!-- 定义标签名 -->
<name>arrTab</name>
<!-- 定义标签处理类 -->
<tag-class>ns.taglib.arrayListShowTab</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
<!-- 配置标签属性: -->
<attribute>
<name>arr</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>map</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
    </taglib>

jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="arrtab"  uri="/arrTab"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
ArrayList arrlist = new ArrayList();
Map map = new HashMap();
map.put("id","1");map.put("name","jim");map.put("sex","boy");map.put("phone","15130189936");map.put("mes","hello world");
arrlist.add(map);
Map mapp = new HashMap();
mapp.put("mes","信息");
mapp.put("id","编号");// 编号
mapp.put("name","姓名");
mapp.put("sex","性别");
mapp.put("phone","电话");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'tagTs.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>
    <arrtab:arrTab
    arr="<%=arrlist %>"
    map="<%=mapp %>"
    />
  </body>
</html>

参考资料:
http://book.csdn.net/bookfiles/832/10083225007.shtml
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics