2005-11-25

Taglib 原理和实现之嵌套和属性读取

来源: 本站收集整理 作者:佚名 评论 0 条
 
  编写 NestedOutputTag 如下:

package diegoyun;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

/**
* @author chenys
*/
public class NestedOutputTag extends BodyTagSupport
{
 private String property = null;

 public void setProperty(String property)
 {
  this.property = property;
 }

 public int doEndTag()throws JspException
 {
  WithTag parent =(WithTag)getParent();
  if(parent==null)
   throw new JspException("Can not find parent Tag ");
   try
   {
    Object propertyValue = PropertyUtils.getProperty(parent.getValue(), property);
    parent.setOutput(propertyValue);
   }
   catch (Exception e)
   {
    throw new JspException(e);
   }
   return EVAL_PAGE;
  }
}

  在包diegoyun下添加一个包vo,在vo下写一个Man类:

package diegoyun.vo;

/**
* @author chenys
*/
public class Man
{
 private String name = null;
 private int age = 0;

 public int getAge()
 {
  return age;
 }
 public void setAge(int age)
 {
  this.age = age;
 }
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
}

  写TLD

<!--WithTag-->
<tag>
<name>with</name>
<tag-class>diegoyun.WithTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!--OutputTag3-->
<tag>
<name>nestedout</name>
<tag-class>diegoyun.NestedOutputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

  写JSP页面

<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>

<html>
<body bgcolor="#FFFFFF">
<%
Man man = new Man();
man.setName("diego");

request.setAttribute("man",man);
%>
Test nested tag:
<br>
<diego:with value="${man}">
<diego:nestedout property="name"/>
</diego:with>
</body>
</html>

  运行页面,则可以看到:


共3页: 上一页 [1] 2 [3] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:对C#中的TreeView添加背景图  
下一篇:叩开C#之门系列之几个重要名词
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号