How to send data from JSP to Action using <s: a> in Struts2?

I want to achieve this title click, the corresponding id is sent to the Action, but the id in the Action is always zero.

The action uses the id to find the corresponding record in the database and map to another jsp.

<s:iterator id="n" value="news">
<tr>
    <td>
    <s:url action="GETNEWS" var="getNews">
        <s:a href="%{getNews}">
            <s:property value="#n.title" />
            <s:hidden name="id" value="#n.id" />
        </s:a>
    </s:url>
    </td>
</tr>
</s:iterator>

      

+3


source to share


2 answers


id

is <s:iterator>

deprecated (unless you are using the eonic version). Use instead var

.

Use <s:param>

inside <s:url>

(and, for better coding, don't forget to include the namespace).
Then point <s:url>

from <s:a>

.



Finally, make sure you have the correct setter in action.

<s:iterator var="n" value="news">
<tr>
    <td>

        <s:url action="GETNEWS" namespace="/" var="getNews">
            <s:param name="id">
                <s:property value="#n.id" />
            </s:param>
        </s:url>

        <s:a href="%{getNews}">
            <s:property value="#n.title" />
        </s:a>

    </td>
</tr>
</s:iterator>

      

+3


source


Use a tag <s:param>

to send a value to the server. So your code will look like



<s:iterator id="n" value="news">
<tr>
    <td>
    <s:url action="GETNEWS" var="getNews">
        <s:param name="id"><s:property value="#n.id"/></s:param>
        <s:a href="%{getNews}">
            <s:property value="#n.title"/>
        </s:a>
    </s:url>
    </td>
</tr>
</s:iterator>

      

0


source







All Articles