0

I need to print catalogo.id because I need to get catalogo object from its id. How can I get it, because I have an iterator? Now I print action?idprodotto=XX&idcatalogo=

jsp

<s:iterator value="catalogo">
    <s:iterator value="prodotti">
        <tr style="background-color: #99CCFF">
            <td><s:property value="id" /></td>
            <td><s:property value="descrizione" /></td>
            <td><s:property value="prezzo" /></td>
            <td><s:property value="nPezziVenduti" /></td>
            <td><a
                href='<s:url action="cancellaProdottoDaCatalogo.action" >
            <s:param name="idprodotto"><s:property value="id"/></s:param>
            <s:param name="idcatalogo"><s:property value="catalogo.id"/></s:param></s:url>'>rimuovi
                    dal catalogo</a></td>
        </tr>
    </s:iterator>
</s:iterator>

UPDATE Now, I got id of catalogo but in the action, I have a NullPointer error, I think that with [1].id i pass only string and not object. How can solve this problem?

NEW JSP

<s:iterator value="catalogo">
    <s:iterator value="prodotti">
        <tr style="background-color: #99CCFF">
            <td><s:property value="id" /></td>
            <td><s:property value="descrizione" /></td>
            <td><s:property value="prezzo" /></td>
            <td><s:property value="nPezziVenduti" /></td>
            <td><a
                href='<s:url action="cancellaProdottoDaCatalogo.action" >
            <s:param name="idprodotto"><s:property value="id"/></s:param>
            <s:param name="idcatalogo"><s:property value="[1].id"/></s:param></s:url>'>rimuovi
                    dal catalogo</a></td>
        </tr>
    </s:iterator>
</s:iterator>

ACTION METHOD

public String cancellaProdottoDaCatalogo(){
    System.out.println(catalogo.getDescrizione());
    Catalogo tmp = catalogoDAO.getCatalogo(catalogo.getId());
    for(Prodotto p:catalogo.getProdotti()){
        tmp.rimuoviProdotto(p);
    }
    catalogoDAO.aggiornaCatalogo(catalogo);
    return SUCCESS;
}
Manlio
  • 41
  • 1
  • 8

1 Answers1

0

After reading the updated question I think you should use the var-attribute, as @A Lee already answered in the comments.

You have a nested iteration and if you want to access the first iterator you have to use some sort of reference to find the values from the first iterator. And var gives you the reference to the current element in the outer loop and pushes the reference into the value stack as c.

Same with the inner loop p references the current element in the inner loop.

As a side note: the struts iterator pushes the elementes of the current element into the value stack, so that you can easily just use id, descrizione etc. See the third example in the struts docs here.

Example:

<s:iterator value="catalogo" var="c">
   <s:iterator value="#c.prodotti" var="p">
   <s:url action="..." var="urlCancel">
      <s:param name="idprodotto" value="#p.id"/>
      <s:param name="idcatalogo" value="#c.id"/>
   </s:url>
   <td>Catalogo Id: <s:property varlue="#c.id"/></td>
   <td>Prodotti Id: <s:property varlue="#p.id"/></td>
   <!-- other columns -->
   <td><a href="<s:property value='urlCancel'/>Link</a></td>"
   </s:iterator>
</s:iterator>

Note the # before the refernce name, it's required in this case to search the variable in the context.

beendr
  • 654
  • 7
  • 21