Mega Code Archive

 
Categories / Java Tutorial / JSP
 

Repeat Tag

/* * 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>