简体   繁体   English

如何将数据从JSP发送到Action with <s:a> 在Struts2?

[英]How to send data from JSP to Action with <s:a> in Struts2?

I want to achieve that clicking the title, the matching id is sent to the Action, but id in the Action is always zero. 我希望实现单击标题,匹配的id被发送到Action,但Action中的id始终为零。

The Action uses the id to search the matching record in database and show in another jsp. Action使用id搜索数据库中的匹配记录并显示在另一个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>

id in <s:iterator> is deprecated (unless you are using an aeon-old version). 不推荐使用<s:iterator> id (除非您使用的是旧版本)。 Use var instead. 改用var

Use <s:param> inside <s:url> (and, for a better coding, remember to specify the namespace). <s:url>使用<s:param> (为了更好的编码,请记住指定命名空间)。
Then reference the <s:url> from <s:a> . 然后从<s:a>引用<s:url> <s:a>

Finally, ensure you have the right Setter in your action. 最后,确保您的行动中有正确的Setter。

<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>

Use <s:param> tag to send value to server.So your code would look like 使用<s:param>标签向服务器发送值。所以你的代码看起来像

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM