自定义标签库读取list输出

jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/mylist" prefix="my5" %>

<%
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 'list.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>
     <%
        List list=new ArrayList();
        list.add("aaaa");
        list.add("sss");
        list.add("ddd");
        list.add("fff");
        request.setAttribute("listObj",list);
      %>
      <hr>
     
      <%
      List list1=(List)request.getAttribute("listObj");
      Iterator iterator=list1.iterator();
      while(iterator.hasNext()){
      String str=(String)iterator.next();
      request.setAttribute("strstr",str);
     
       %>
       ${strstr} <br>
      <%
      }
       %>
       <hr/>
       <!-- 用自定义标签库表示 -->
       <my5:myList  items="${listObj}" var="yyyy" >
         ${yyyy} ##<br/>
       </my5:myList>
  </body>
</html>




java
package com.cgm.tlist;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

public class MyList extends SimpleTagSupport {
private List items;   
private String var;

public List getItems() {
return items;
}

public void setItems(List items) {
this.items = items;
}

public String getVar() {
return var;
}

public void setVar(String var) {
this.var = var;
}

@Override
public void doTag() throws JspException, IOException {

System.out.println("============"+items);
System.out.println("-============="+var);

Iterator iterator =items.iterator();

while (iterator.hasNext()) {
             String str=(String) iterator.next();
             this.getJspContext().setAttribute(var, str);
             this.getJspBody().invoke(null);
            
            
}








}

@Override
protected JspFragment getJspBody() {
// TODO Auto-generated method stub
return super.getJspBody();
}

@Override
protected JspContext getJspContext() {
// TODO Auto-generated method stub
return super.getJspContext();
}

@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return super.getParent();
}

@Override
public void setJspBody(JspFragment jspBody) {
// TODO Auto-generated method stub
super.setJspBody(jspBody);
}

@Override
public void setJspContext(JspContext pc) {
// TODO Auto-generated method stub
super.setJspContext(pc);
}

@Override
public void setParent(JspTag parent) {
// TODO Auto-generated method stub
super.setParent(parent);
}

}





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">
<tlib-version>1.0</tlib-version>
<short-name>my5</short-name>
<uri>http://www.cgmcc.com/jsp/mylist</uri>

   <!--
  <my5:myList  items="${listObj}" var="yyyy" >
   -->
 
 
 
  <tag>
    <name>myList</name>
    <tag-class>com.cgm.tlist.MyList</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>


</taglib>

你可能感兴趣的:(自定义标签)