文章標題: 使用jsp自定義標簽庫實現數據列表顯示模擬cms4j中的標簽庫效果 作者: javaboy2012 Email:[email protected] qq: 1046011462 cms4j中調用方式: 下面例子實現類似效果: 運行效果: 具體如下: jsp調用代碼: <%@ page language="java" import="java.util.*,com.yanek.cms.vo.*" pageEncoding="UTF-8"%> <%@ taglib uri="/tags/my-cms" prefix="myTag" %> <body> <myTag:articleListTag cateid="1"> <%=article_info.getId() %>------ <%=article_info.getTitle() %> <br> ${article_info.id }---- ${article_info.title }<br> </myTag:articleListTag> <hr> <myTag:articleListTag cateid="2"> <%=article_info.getId() %>------ <%=article_info.getTitle() %> <br> ${article_info.id }---- ${article_info.title }<br> </myTag:articleListTag> </body> </html> 標簽庫定義 <!-- articleListTag start --> <tag> <name>articleListTag</name> <tag-class>com.yanek.cms.tag.ArticleTag</tag-class> <body-content>jsp</body-content> <variable> <name-given>article_info</name-given> <!--<name-from-attribute>name</name-from-attribute>--> <variable-class>com.yanek.cms.tag.Article</variable-class> <declare>true</declare> <scope>NESTED</scope> </variable> <attribute> <name>cateid</name> <required>true</required> </attribute> </tag> <!-- articleListTag end --> 標簽庫類 package com.yanek.cms.tag; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class ArticleTag extends BodyTagSupport { private Iterator it;// 要迭代的對象 private int cateid; // 文章類別id public final static String name = "article_info"; @Override public int doEndTag() throws JspException { try { if (bodyContent != null) { bodyContent.writeOut(bodyContent.getEnclosingWriter()); } } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } @Override public int doStartTag() throws JspException { //這裡根據文章分類,構造不同的列表數據,實際可以根據數據庫獲取 List<Article> articles = new ArrayList<Article>(); if (cateid == 1) { articles.add(new Article(1, "asp")); articles.add(new Article(2, "jsp")); } else { articles.add(new Article(3, "php")); articles.add(new Article(4, "java")); } it = articles.iterator(); if (it == null) { return SKIP_BODY; } else { return continueNext(); } } private int continueNext() { if (it.hasNext()) { pageContext.setAttribute(name, it.next(), pageContext.PAGE_SCOPE); return EVAL_BODY_TAG; } else { return SKIP_BODY; } } @Override public int doAfterBody() { return continueNext(); } public int getCateid() { return cateid; } public void setCateid(int cateid) { this.cateid = cateid; } } 標簽庫引用實體類 package com.yanek.cms.tag; public class Article { private int id; private String title; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Article(int id, String title) { super(); this.id = id; this.title = title; } public Article() { super(); this.id = id; this.title = title; } } 幾點說明:標簽調用是顯示list中對象時采用的腳本變量名字article_info 是在標簽庫定義文件和標簽庫類中定義的,如下 <variable> <name-given>article_info</name-given> <!--<name-from-attribute>name</name-from-attribute>--> <variable-class>com.yanek.cms.tag.Article</variable-class> <declare>true</declare> <scope>NESTED</scope> </variable> 標簽庫類中 public final static String name = "article_info"; pageContext.setAttribute(name, it.next(), pageContext.PAGE_SCOPE); 顯示列表中對象的方式有2種方式: 調用類的方法: 在eclipse中可以方法提示。 <%=article_info.getId() %>---- <%=article_info.getTitle() %> jstl顯示: ${article_info.id }---- ${article_info.title }