【问题标题】:Modify record using Struts2使用Struts2修改记录
【发布时间】:2020-03-29 17:54:11
【问题描述】:

我有一个包含节目列表的表格,我的想法是有一个允许修改每个节目的按钮:

         ...

         <s:iterator value="%{listShow}" var="show">
            <tr>
                <td><s:property value="showId"></s:property></td>
                <td><s:property value="showName"></s:property></td>
                <td><s:property value="showDate"></s:property></td>
                <td><s:property value="showPrice"></s:property></td>
                 <td><s:form action="goModify">
                        <s:submit value="Modify"></s:submit>
                        <s:hidden name="showId"></s:hidden>
                        <s:hidden name="showName"></s:hidden>
                        <s:hidden name="showDate"></s:hidden>
                        <s:hidden name="showPrice"></s:hidden>
                    </s:form></td>

            </tr>
        </s:iterator>


        ...

“goModify”唯一的操作是重定向到我想要更改数据的 modify.jsp 文件:

    <s:form action="modifyAction">

        <s:textfield label="ID" name="showId" value="%{showId}"></s:textfield>
        <s:textfield label="Show Name" name="showName" value="%{showName}></s:textfield>
        <s:textfield label="Date" name="showDate" value="%{showDate}></s:textfield>
        <s:textfield label="Price" name="showPrice"value="%{showPrice}></s:textfield>

        <s:submit value="Modificar"></s:submit>

   </s:form>

我看不到填充字段的问题。

【问题讨论】:

标签: html forms struts2 struts


【解决方案1】:

您只是为隐藏字段指定名称,而不是值。您可以使用属性key 而不是name。它将自动生成具有正确名称和值的 HTML。

<s:iterator value="%{listShow}" var="show">
    <tr>
        <td><s:property value="showId" /></td>
        <td><s:property value="showName" /></td>
        <td><s:property value="showDate" /></td>
        <td><s:property value="showPrice" /></td>
        <td>
            <s:form action="goModify">
                <s:submit value="Modify" />
                <s:hidden key="showId" />
                <s:hidden key="showName" />
                <s:hidden key="showDate" />
                <s:hidden key="showPrice" />
            </s:form>
        </td>
    </tr>
</s:iterator>

如果操作 goModify 没有将参数存储在值堆栈中(例如,通过将它们存储在属性中),您可能必须更改 JSP 才能直接访问请求参数。

<s:form action="modifyAction">
    <s:textfield label="ID" name="showId" value="%{#parameters.showId}" />
    <s:textfield label="Show Name" name="showName" value="%{#parameters.showName} />
    <s:textfield label="Date" name="showDate" value="%{#parameters.showDate} />
    <s:textfield label="Price" name="showPrice" value="%{#parameters.showPrice} />
    <s:submit value="Modificar"></s:submit>
</s:form>

但是,第二个 JSP 的最佳解决方案也是使用 key 而不是 namevalue。但是你必须确保值堆栈中的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多