使用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 (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 }
下一篇: 用缓冲技术提高JSP应用的性能和稳定性