您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

jsp tag实例

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 23:30 点击:

 

 1、问题:在request里的People 对象,有个属性叫men ,men 是一个Collection ,有许多个man 。现在,把collection里的man的名字都显示出来。

  显然,这是一个嵌套Tag的问题。有三个Tag互相作用:最外层的Tag找到People对象,中间的Tag取得Collection,子Tag负责打印。

  例如:

<diego:withObject value=$>

<diego:withCollection property=men>

<diego:elementout property=name/>

</diego:withCollection>

</diego:withObject>

  思路如下:

  1) 编写WithObjectTag,负责从El表达式中取得对象

  2) 编写WithCollectionTag,负责从对象中取得Collection ,遍历Collection ,每遍历一次Collection ,执行一次body

  3) 编写ElementoutTag ,把Collection 中每个men对象的name 打印出来

  2. 完整程序如下:

  在上例的diegoyun.vo包内,编写People 类

package diegoyun.vo;

import java.util.Collection;

public class People

{

private Collection men = null;

public Collection getMen()

{

return men;

}

public void setMen(Collection men)

{

this.men = men;

}

}

  编写withObject ,这是从request里取得People对象的最外层Tag

package diegoyun;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

public class WithObjectTag extends BodyTagSupport

{

private Object value = null;

 public Object getValue()

{

return value;

}

public void setValue(Object value)throws JspException

{

this.value = ExpressionEvaluatorManager.evaluate(value, value.toString(), Object.class, this, pageContext);

}

public int doStartTag()

{

return EVAL_BODY_INCLUDE;

}

public int doEndTag()throws JspException

{

return EVAL_PAGE;

}

  编写WithCollectionTag,该Tag负责取得Collection,并遍历执行子Tag

package diegoyun;

import java.util.Collection;

import java.util.Iterator;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class WithCollectionTag extends BodyTagSupport {

private Object element = null;

 private Collection list = null;

 private Iterator iterator = null;

 public Object getElement() {

return element;

}

 public void setProperty(String property) throws JspException {

//取得父Tag对象,并且得到Collection

WithObjectTag parent = (WithObjectTag) getParent();

if (parent == null)

throw new JspException(parent tag is null);

try {

Object propertyValue = PropertyUtils.getProperty(parent.getValue(),property);

this.list = (Collection) propertyValue;

if (list == null)

throw new JspException(Collection is null);

} catch (Exception e) {

throw new JspException(e);

}

}

 public int doStartTag() throws JspException {

//设置第一个元素,然后执行子Tag

iterator = list.iterator();

if (iterator.hasNext())

element = iterator.next();

return EVAL_BODY_INCLUDE;

}

 public int doAfterBody() {

if (iterator.hasNext()) {

//如果还存在子元素,设置子元素,并且再次执行子Tag

//循环由此而来

//否则不再执行子Tag

element = iterator.next();

return EVAL_BODY_AGAIN;

}

else

return EVAL_PAGE;

}

}

  编写ElementOutputTag

package diegoyun;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUt

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • JSP与Servlet
      • 自己动手写MiniBBS系列(基本篇)之用户登录
      • JSP取当前日期
      • JDBC 入门(一)
      • 打开一个jsp页面默认查询所有数据,调用action
      • 使用JSP标签库验证用户的输入(2)完
      • WIN98/2000下的jsp服务器
      • 自定义JSP标签(tag)浅议
      • Struts学习傻瓜式入门篇
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1