You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Peter Cheung <pe...@home.com> on 2000/07/14 07:21:36 UTC

iterate Tag

    I was trying to replace the following piece of JSP with Struts tags.

<%int suffix     = 0;
    Enumeration enum = PhoneList.elements();
    Phone phone = null;
    while (enum.hasMoreElements()) {
      phone = (String)enum.nextElement();
	suffix++;
%>

<TR>
  <TD>
    <%=phone.getManufacturer()%>
  </TD>
  <TD>
    <%=phone.getModel()%>
  </TD>
  <TD>
    <%=phone.getPrice()%>
  </TD>
  <TD ALIGN="center"><INPUT TYPE="CHECKBOX" NAME="pname<%=suffix%>"
VALUE="<%=phone.getId()")%>">
  </TD>
} %>

    However, I was not able to assign the phone id as the value of
"CHECKBOX". The following is what the Strut tag looks like:

      <struts:iterate id="phones" name="phoneResult">
        <tr>
          <td align="left">
            <struts:htmlProperty name="phones" property="manufacturer"/>
          </td>
          <td align="left">
            <struts:htmlProperty name="phones" property="model"/>
          </td>
          <td align="left">
            <struts:htmlProperty name="phones" property="price"/>
          </td>
          <% suffix++; %>
          <td ALIGN="center">
            <INPUT TYPE="CHECKBOX" NAME="pname<%=suffix%>"
VALUE="<%=phones.getId()%>"></TD>
          </td>
        </tr>
      </struts:iterate>

    Tomcat gave me an error:
org.apache.jasper.JasperException: Unable to compile class for
JSPD:\jakarta\jakarta-tomcat\work\localhost_8080%2Fi010n\_0002fPhoneResult_0
002ejspPhoneResult_jsp_5.java:439: Method getId() not found in class
java.lang.Object.
                  out.print(phones.getId());

    phones is a collection, and getId() is one of the method of the elements
in phones collection. I thought I could use phones to refer an element in
the collection inside the 'iterate' tag, but Tomcat did not like it, which
is legitimate.

    I wonder if someone can give me some idea of how to successfully
replacing the JSP code mentioned above. Thx in advance.


Re: iterate Tag

Posted by Pasi Salminen <pa...@profithome.com>.
As the error message says:

Method getId() not found in class java.lang.Object.
                  out.print(phones.getId());

<struts:iterate id="phones" name="phoneResult">

How could the code know that "phones", declared in iterate tag, is of type
PhoneList? It doesn't, so you have to cast it in order to use getId method!
Here's one recommedation which works for me: if you have compilation problems or
strange behaviour, look the actual servlet source in work directory and look
what's really happening there. It's the ultimate documentation, although in a
little bit cryptic form.

Greetings,

Paci

Peter Cheung wrote:

>     I was trying to replace the following piece of JSP with Struts tags.
>
> <%int suffix     = 0;
>     Enumeration enum = PhoneList.elements();
>     Phone phone = null;
>     while (enum.hasMoreElements()) {
>       phone = (String)enum.nextElement();
>         suffix++;
> %>
>
> <TR>
>   <TD>
>     <%=phone.getManufacturer()%>
>   </TD>
>   <TD>
>     <%=phone.getModel()%>
>   </TD>
>   <TD>
>     <%=phone.getPrice()%>
>   </TD>
>   <TD ALIGN="center"><INPUT TYPE="CHECKBOX" NAME="pname<%=suffix%>"
> VALUE="<%=phone.getId()")%>">
>   </TD>
> } %>
>
>     However, I was not able to assign the phone id as the value of
> "CHECKBOX". The following is what the Strut tag looks like:
>
>       <struts:iterate id="phones" name="phoneResult">
>         <tr>
>           <td align="left">
>             <struts:htmlProperty name="phones" property="manufacturer"/>
>           </td>
>           <td align="left">
>             <struts:htmlProperty name="phones" property="model"/>
>           </td>
>           <td align="left">
>             <struts:htmlProperty name="phones" property="price"/>
>           </td>
>           <% suffix++; %>
>           <td ALIGN="center">
>             <INPUT TYPE="CHECKBOX" NAME="pname<%=suffix%>"
> VALUE="<%=phones.getId()%>"></TD>
>           </td>
>         </tr>
>       </struts:iterate>
>
>     Tomcat gave me an error:
> org.apache.jasper.JasperException: Unable to compile class for
> JSPD:\jakarta\jakarta-tomcat\work\localhost_8080%2Fi010n\_0002fPhoneResult_0
> 002ejspPhoneResult_jsp_5.java:439: Method getId() not found in class
> java.lang.Object.
>                   out.print(phones.getId());
>
>     phones is a collection, and getId() is one of the method of the elements
> in phones collection. I thought I could use phones to refer an element in
> the collection inside the 'iterate' tag, but Tomcat did not like it, which
> is legitimate.
>
>     I wonder if someone can give me some idea of how to successfully
> replacing the JSP code mentioned above. Thx in advance.