自定义标签:脚本变量 : 标签 « JSP技术 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » JSP技术 » 标签屏幕截图 
自定义标签:脚本变量


<!-- this must be added to the web application's web.xml -->

<taglib>
  <taglib-uri>/java2s</taglib-uri>
  <taglib-location>/WEB-INF/java2s.tld</taglib-location>
</taglib>

// create File:java2s.tld in the /WEB-INF/
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
   "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

    <!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>Java2s Simple Tags</short-name>

  <!-- this tag lists random numbers; the HTML is hard-coded within
       the tag handler
    -->
 
  <!-- this tag exports random numbers in a named array -->
  <tag>
    <name>defineObjects</name>
    <tag-class>com.conygre.jspdevhandbook.chapter9.EmptyTagWithAttrsExport</tag-class>
    <tei-class>com.conygre.jspdevhandbook.chapter9.EmptyTagExtraInfo</tei-class>
    <body-content>empty</body-content>
    <!--
         Use this block instead of a TEI class

    <variable>
      <name-from-attribute>name</name-from-attribute>
      <variable-class>int []</variable-class>
      <declare>TRUE</declare>
      <scope>AT_END</scope>
    </variable>
    -->
    
    <attribute>
      <name>howMany</name>
    </attribute>
    <attribute>
      <name>name</name>
    </attribute>
  </tag>

</taglib>
//compile the following code into WEB-INF\classes\com\java2s
package com.java2s;

import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;

public class EmptyTagExtraInfo extends TagExtraInfo
{
  public VariableInfo[] getVariableInfo(TagData tagData)
  {
    String exportedArrayName = (StringtagData.getAttribute("name");
    VariableInfo exportedArrayInfo = new VariableInfo(exportedArrayName,
                                                      "int []",
                                                      true,
                                                      VariableInfo.AT_END);

    return new VariableInfo[] {exportedArrayInfo};
  }
}
package com.java2s;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/* This tag handler generates the random numbers. The "howMany" attribute
   specifies how many number to generate and display. The generated array
   of integers is exported with nested visibility, through an array named
   byte the "name" attribute.
 */

public class EmptyTagWithAttrsExport extends BodyTagSupport
{
  // Code to implement the "howMany" attribute
  private int howMany;
  public int getHowMany()
  {
    return howMany;
  }
  public void setHowMany(int i)
  {
    howMany = i;
  }

  // Code to implement the "name" attribute
  private String exportedArrayName;
  public String getName()
  {
    return exportedArrayName;
  }
  public void setName(String s)
  {
    exportedArrayName = s;
  }
  
  public int doStartTag() throws JspException
  {
    int[] outputArray = new int[howMany];
    System.out.println("Generating " + howMany + " numbers");

    for int i=0; i<howMany; i++ )
    {
      outputArray[i(int) (Math.random() 10);
    // end of for ()

    pageContext.setAttribute(exportedArrayName, outputArray);

    return SKIP_BODY;
  }

  /*  public int doEndTag() throws JspException
  {
    return super.doEndTag();
  }*/
}



// start comcat and load the following jsp page in browser
<%@ taglib uri="/java2s" prefix="java2s" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
  <head>
    <title>A custom tag: scripting variable</title>
  </head>
  <body>
    output:

    <java2s:defineObjects howMany="5" name="numbers" />
    <ul>
      <c:forEach items="${numbers}" var="currentNumber">
        <li>
          <c:out value="${currentNumber}" />
        </li>
      </c:forEach>
    </ul>
  </body>
</html>



           
       
Related examples in the same category
1. 您自己的简单的JSP标签
2. 创建您自己的标记:一个自定义标签属性
3. A custom tag that has neither attributes nor body content.
4. 自定义标签:空与属性
5. 撰写您自己的标签
6. 标识标签
7. 自定义标签:空
8. 标记生命周期属性
9. 自定义标签:迭代
10. JSP Simple Tags
11. JSP的标签:先进的标签JSP的标签:先进的标签
12. JSP的经典标签JSP的经典标签
13. JSP的标记库和JSTLJSP的标记库和JSTL
14. JSP的指令: HTML标记
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.