Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Custom for loop tag

843838Nov 19 2005 — edited Nov 20 2005
Hi all,

I am trying to implement a custom "for loop tag". But it seems to me that there is a type conversion problem. I've tried many ways to fixed it but I still cannot get it to work. I've dug through my books, searched the internet and this forum but I can't found anythig useful. I am really lost now! Can anyone help me with this? Many many many thanks!!

loopTag.jsp
<%@ taglib prefix="myTag" uri="/WEB-INF/myTag.tld" %>
<% int num=5; %>

<myTag:loop index="i" count="<%=num%>">
	body1here: i expr: <%=i%> i property: <jsp:getProperty name="i" property="value"/> <br>
</myTag:loop>
myTag.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>simple</shortname>
 
  <tag>
    <name>loop</name>
    <tagclass>myTag.LoopTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <info>for loop</info>
    <attribute>
        <name>index</name>
        <required>true</required>
    </attribute>
    <attribute>
        <name>count</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>
LoopTag.java
package myTag;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Hashtable;
import java.io.Writer;
import java.io.IOException;

public class LoopTag extends BodyTagSupport {    
    String index;
    int count;
    int i=0;

    public void setIndex(String index){
      this.index=index;
    }
    public void setCount(String count){
      this.count=Integer.parseInt(count);
    }

    public int doStartTag() throws JspException {
        return EVAL_BODY_TAG;
    }

    public void doInitBody() throws JspException {
        pageContext.setAttribute(index, i);
        i++;
    }
    
    public int doAfterBody() throws JspException {
        try {
            if (i >= count) {
               bodyContent.writeOut(bodyContent.getEnclosingWriter());
                return SKIP_BODY;
            } 
						else{
							pageContext.setAttribute(index, i);
						}
						i++;
            return EVAL_BODY_TAG;
        } 
				catch (IOException ex) {
            throw new JspTagException(ex.toString());
        }
    }
}
Error message:
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 4 in the jsp file: /loopTag.jsp
Generated servlet error:
The method setCount(String) in the type LoopTag is not applicable for the arguments (int)

An error occurred at line: 5 in the jsp file: /loopTag.jsp
Generated servlet error:
i cannot be resolved


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.11 logs.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 18 2005
Added on Nov 19 2005
8 comments
180 views