使用jsp自定义标签库实现数据列表显示模拟cms4j中的标签库效果
来源:互联网 发布:gxsec软件 编辑:程序博客网 时间:2024/06/04 17:58
文章标题: 使用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自定义标签库实现数据列表显示模拟cms4j中的标签库效果
- jsp自定义标签库实现数据列表显示
- Eclipse+JSP使用自定义标签库查询出MySQL表中的数据显示在页面
- JSP 自定义标签库
- Jsp 自定义标签库
- JSP自定义标签库
- JSP自定义标签库
- JSP自定义标签库
- JSP自定义标签库
- jsp 自定义标签库
- 自定义标签库jsp
- 【jsp】使用SimpleTagSupport自定义标签库
- 自定义JSP标签库及Properties使用
- JSP自定义标签库的使用
- jsp自定义数据标签
- Jsp中的自定义标签
- JSP中的自定义标签
- JSP中的自定义标签
- 励志博文:从1.5k到18k, 一个程序员的5年成长之路
- drawSelectorOnTop的作用
- [AndEngine学习教程] 第10节 box2D 物理碰撞系统
- What is a Class A audio amplifier?
- lamp
- 使用jsp自定义标签库实现数据列表显示模拟cms4j中的标签库效果
- gcc 安装详解
- sgip错误代码
- Flash字体嵌入方法总结—(1)基础篇
- android源码编译
- js重定向---实现页面跳转的几种方式
- 编写一个C语言语句,要求输出这个程序需要运行的时间(用clock()做) 该怎么写?
- Java访问权限修饰符
- Hadoop中HDFS常用命令