日期:2014-05-16 浏览次数:20459 次
文章标题: 使用jsp自定义标签库实现数据列表显示模拟cms4j中的标签库效果
作者: javaboy2012
Email:yanek@163.com
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 (bod