Search code examples
html-tablestrutstaglib

"onclick=submit()" in a <table> created through struts taglib <html:forEach>


I'd like to send information to an ActionFormBean using a jsp page that contains a structured code like this:

<table>
<thead>
    <tr>
        <th>ID Turno</th>
        <th>Data e Ora</th>
        <th>Sede turno</th>
    </tr>
</thead>
<tbody>
    <html:form action="/dettagliTurno">
    <c:forEach var="ris" items="${usersession.searchResult}" >
        <tr>
            <td><c:out value="${ris.idTurno}"/></td>
            <td><c:out value="${ris.dataOra}"/></td>
            <td><c:out value="${ris.luogo}"/></td>
            <td>
                <html:hidden property="id" value="${ris.idTurno}"/>
                <html:submit value="Dettagli" property="id" />
            </td>
        </tr>
    </c:forEach>
    </html:form>
</tbody>           
</table>

The only one information that I need to send to the ActionFormBean is "id" but if click on the button "Dettagli" of the second/third row (of the example showed in this image) the value sent to the ActionForm is always the one of the first row (that is '5')!

How can I solve this problem and set the correct value of id according to the row-button "Dettagli" selected?


Solution

  • GOT IT!

    I solved the problem by changing code this way:

    <c:forEach var="ris" items="${usersession.searchResult}" >
    <tr>
        **<html:form action="/dettagliTurno">**
            <td><c:out value="${ris.idTurno}"/></td>
            <td><c:out value="${ris.dataOra}"/></td>
            <td><c:out value="${ris.luogo}"/></td>
            <td>
                <html:hidden property="id" value="${ris.idTurno}"/>
                <html:submit value="Dettagli" property="id" />
            </td>
        **</html:form>**
    </tr>
    

    With this version, form set the correct value of "idTurno" and properly send it to ActionForm.

    Hope it will be helpfulto someone with the same problem of mine.

    Calc