Repeat Tag : Custom Tag « JSP « Java Tutorial






/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package beans;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.HashMap;
import java.io.IOException;

/**
 * SimpleTag handler that accepts a num attribute and 
 * invokes its body 'num' times.
 */
public class RepeatSimpleTag extends SimpleTagSupport {
    private int num;

    public void doTag() throws JspException, IOException {
        for (int i=0; i<num; i++) {
            getJspContext().setAttribute("count", String.valueOf( i + 1 ) );
      getJspBody().invoke(null);
        }
    }

    public void setNum(int num) {
  this.num = num;
    }
}

web.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

  <taglib>
      <taglib-uri>
         http://jakarta.apache.org/tomcat/jsp2-example-taglib
      </taglib-uri>
      <taglib-location>
         /WEB-INF/tags/jsp2-example-taglib.tld
      </taglib-location>
  </taglib>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.timeZone</param-name>
    <param-value>US/Central</param-value>
  </context-param>

  <context-param>
    <param-name>database-driver</param-name>
    <param-value>org.gjt.mm.mysql.Driver</param-value>
  </context-param>

  <context-param>
    <param-name>database-url</param-name>
    <param-value>
    jdbc:mysql://localhost/forum?user=forumuser</param-value>
  </context-param>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
    <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
    <taglib-location>/WEB-INF/c-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/sql.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
    <taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/x.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
    <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>
    http://java.jeffheaton.com/taglib/jstl/forum</taglib-uri>
    <taglib-location>/WEB-INF/forum.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/tlt</taglib-uri>
    <taglib-location>/WEB-INF/taglib.tld</taglib-location>
  </taglib>
</web-app>

jsp2-example-taglib.tld

<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<%@ taglib prefix="tags" uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib"%>
<html>
  <head>
    <title>JSP 2.0 Examples - Repeat SimpleTag Handler</title>
  </head>
  <body>
    <h1>JSP 2.0 Examples - Repeat SimpleTag Handler</h1>
    <hr>
    <P>This tag handler accepts a "num" parameter and repeats the body of the
    tag "num" times.  It's a simple example, but the implementation of 
    such a tag in JSP 2.0 is substantially simpler than the equivalent 
    JSP 1.2-style classic tag handler.</p>
    <P>The body of the tag is encapsulated in a "JSP Fragment" and passed
    to the tag handler, which then executes it five times, inside a 
    for loop.  The tag handler passes in the current invocation in a
    scoped variable called count, which can be accessed using the EL.</p>
    <br>
    <b><u>Result:</u></b><br>
    <mytag:repeat num="5">
      Invocation <c:out value="${count}"/> of 5<br>
    </mytag:repeat>
  </body>
</html>
  Download:  RepeatTag.zip( 1,083 k)








23.58.Custom Tag
23.58.1.Custom Tag Support
23.58.2.Custom Tag For Outputing Message To Console
23.58.3.Custom Tag For Iteration
23.58.4.Jars For Custom Tag Development
23.58.5.Custom Tag With Property
23.58.6.Custom Tag Web XML Tag Lib
23.58.7.Custom Tag To Insert Text
23.58.8.Custom Tag To Get Parameter From PageContext
23.58.9.Extends Tag Support DoEndTag
23.58.10.Reference Tag Using Tag Dir
23.58.11.Repeat Tag
23.58.12.Custom Tag For Shuffling Color
23.58.13.Custom Tag For Display message
23.58.14.Custom Tag Without TLD
23.58.15.Custom Tag With Attributes
23.58.16.Custom Tag Web URI Definition
23.58.17.Custom Tag Using Beans
23.58.18.Custom Tag Setup Environment
23.58.19.Custom Tag Set Bean Value
23.58.20.Custom Tag Dynamic Attributes
23.58.21.Custom Tag As Template
23.58.22.Custom Tag Accepting Fragments