java 使用显示标签从 JSP 页面中的一行获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3480980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:01:05  来源:igfitidea点击:

Get value from a row in a JSP page using display tag

javajspweb-applicationsstruts2displaytag

提问by Raymond

Sorry for a bunch of Struts2 / JSP questions, but I have a table generated with Display tag:

抱歉有一堆 Struts2/JSP 问题,但我有一个用Display 标签生成的表:

<display:table name="table" pagesize="10" id="row" requestURI="">
<display:column title="Action">
    <s:form theme="simple">
        <s:submit action="remove" value="Remove" 
        onclick="return confirm('Are you sure you want to delete this item?');"/>
        <s:submit action="displayEdit" value="Edit"/>
    </s:form>
</display:column>
<display:column property="cpc" title="CPC"/>
<display:column property="companyName" title="Company Name"/>
<display:column property="eventType" title="Event Type"/>
<display:column property="industryType" title="Industry Type"/>
<display:column property="previousEvents" />
<display:column property="creditNotifications" />
<display:column property="interimNotifyEnterprise" />
</display:table>

Now I want to be able to delete or edit a certain row (I already have the actions written out), but how would I get the specific data for each row so I can tell my Action class which row to edit or delete?

现在我希望能够删除或编辑某一行(我已经写出操作),但是我如何获取每一行的特定数据,以便我可以告诉我的 Action 类要编辑或删除哪一行?

回答by Shawn D.

I mentioned this in the previous questionyou asked the other day. You just need to something in your row, possibly a hidden field, that can be used to uniquely identify the item.

我在你前几天问的上一个问题中提到了这一点。您只需要行中的某些内容,可能是隐藏字段,可用于唯一标识该项目。

I suggested using an <s:hidden key="rowID" />in the row, which would get submitted with the action. As long as you had a property like that on your object, and in the remove action, you had a setter to receive that value, you could use that to uniquely identify the row.

我建议<s:hidden key="rowID" />在行中使用 ,这将与操作一起提交。只要您的对象上有这样的属性,并且在删除操作中,您就有一个 setter 来接收该值,您就可以使用它来唯一标识该行。

回答by user1159080

use below to get row id. eg i used radio button,

使用下面获取行 id。例如我使用了单选按钮,

<display:column  property="radioButton" title="ID">
                  <input type="radio" value='<%=data_rowNum.intValue()-1%>' name='rowNr' onClick="fnEnableControls()"/>
</display:column>

now u can easily get the value of rowNr using req.getParameterand based on this u can get the record from the list which u have supplied to display tag table.

现在你可以很容易地使用 rowNr 的值,req.getParameter并基于此你可以从你提供给显示标签表的列表中获取记录。

回答by lucentmind

It seems that your table does not have unique identifier field in class Object per Row. You can add field -id in you bean-class that you are going to display. (companyId in Company bean class). So that every row in the table will display unique 'company' object and depending upon 'companyId' field you may edit/delete selected object by passing 'companyId' to Action class.

似乎您的表在 Object per Row 类中没有唯一标识符字段。您可以在要显示的 bean 类中添加字段 -id。(公司 bean 类中的公司 ID)。这样表中的每一行都将显示唯一的“company”对象,并且根据“companyId”字段,您可以通过将“companyId”传递给 Action 类来编辑/删除所选对象。

You may no like to display 'companyId' column in your UI layout to end-user. In that case you can just create hyperlink of 'edit' and 'delete' column using 'companyId' as parameter.

您可能不喜欢在 UI 布局中向最终用户显示“companyId”列。在这种情况下,您可以使用“companyId”作为参数创建“edit”和“delete”列的超链接。

See displaytag tutorial at this sitefor more detail.

有关更多详细信息,请参阅此站点上的displaytag 教程。