javascript 从jsp上的显示标签表中获取行ID,进入struts 2动作类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8504014/
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-26 03:38:39  来源:igfitidea点击:

Get row Id from a display tag table on a jsp, into a struts 2 action class

javascriptjspstruts2actiondisplaytag

提问by kanishk

I am using display tag to display data in a table on a JSP. Now I want to give two links for each row, one for editing & one for deleting the row.

我正在使用显示标记在 JSP 上的表中显示数据。现在我想为每一行提供两个链接,一个用于编辑,一个用于删除该行。

There are some posts on stackoverflow regarding the same([question]: How to use multiple buttons (one on each line) for JSP page using Struts2, [question]: Get value from a row in a JSP page using display tag, [question]: Retrieving Value from Row in Struts2 Table While using Displaytag), but I could not find a solution that works for me.

上有计算器一些帖子就同一([提问]:如何使用Struts2的使用为JSP页面的多个按钮(每行一个),[提问]:使用显示标签在JSP页面的行获取值,[问题]:在使用 Displaytag 时从 Struts2 表中的行检索值),但我找不到适合我的解决方案。

And google gave me ( http://demo.displaytag.org/displaytag-examples-1.2/example-decorator-link.jsp), but it uses URL rewriting which I don't want to use and moreover demonstrates use with struts(I am using struts 2).

谷歌给了我(http://demo.displaytag.org/displaytag-examples-1.2/example-decorator-link.jsp),但它使用了我不想使用的 URL 重写,而且演示了与 struts 一起使用(我正在使用支柱 2)。

My jsp structure and what I am currently trying is :

我的jsp结构和我目前正在尝试的是:

<s:url id="editReport" action="editReport" />
<sd:div href="%{editReport}" listenTopics="editReport" formId="actionForm" showLoadingText="false" preload="false">
    <s:url id="updLists" action="updLists" />
    <sd:div href="%{updLists}" listenTopics="updLists" formId="enterDayReport" showLoadingText="false" preload="false">
        <s:form id="enterDayReport" action="enterDayReport">
            <sd:autocompleter  label="Customer " name="customer" list="customerList"  valueNotifyTopics="updLists" autoComplete="false" searchType="substring"/>
            <sd:autocompleter  label="Contact "  name="contact"  list="contactList"   valueNotifyTopics="updLists" autoComplete="false" searchType="substring"/>
            <s:select          label="Stage "    name="stage"    list="stageList"     headerKey="0" headerValue="Select" />
            <s:select          label="Type "     name="type"     list="typeList"      headerKey="0" headerValue="Select" />
            <sd:datetimepicker label="Date"      name="date"     formatLength="small" displayFormat="dd - MMM - yyyy"/>
            <s:textarea        label="Summary"   name="summary"  cols="40" rows="10"/>
            <s:submit          value="Save Report"/>
        </s:form>
    </sd:div>
</sd:div>

<s:url id="deleteReport" action="deleteReport" />
<sd:div href="%{deleteReport}" listenTopics="deleteReport" formId="actionForm" showLoadingText="false" preload="false">
    <disp:table name="dayReportsList" export="true" class="dataTable">
        <disp:column property="contactCode" title="Contact"/>
        <disp:column property="customerCode" title="Customer"/>
        <disp:column property="stage" title="Stage"/>
        <disp:column property="type" title="Type"/>
        <disp:column property="summary" title="Summary"/>
        <disp:column property="reportDate" title="Date" format="{0,date,dd-MMM-yyyy}" />
        <disp:column property="rowId" href="%{editReport}" paramId="rowID" paramProperty="rowId" title="Action">
            <s:form id="actionForm" name="actionForm">
                <s:hidden id="rowId" name="rowId" value="%{rowId}"/>  // This is not getting populated.
                <s:a onclick="dojo.event.topic.publish('editReport')">Edit<s:property value="rowId"/></s:a><br>
                <s:a onclick="dojo.event.topic.publish('deleteReport')">Delete</s:a>
            </s:form>
        </disp:column>
    </disp:table>
</sd:div>

Here the only problem I am facing is that the hiddenfield in the display tag table is not getting populated with the "rowId" value which is a part of "dayReportsList".

这里我面临的唯一问题是hidden显示标记表中的字段没有填充“rowId”值,该值是“dayReportsList”的一部分。

The idea here is that if a user clicks on edit, the data for the row gets populated in the form for editing. And if the user clicks delete, the row gets deleted from the database and the display table gets updated on JSP.

这里的想法是,如果用户单击编辑,该行的数据将填充到表单中以进行编辑。如果用户单击删除,该行将从数据库中删除,并且显示表在 JSP 上更新。

Please advise.

请指教。

Thanks!!

谢谢!!

采纳答案by JB Nizet

The current element of the list is available through the page context attribute "foobar" if the display:tabletag has the attribute uid="foobar"(or id="foobar"). See http://www.displaytag.org/1.2/displaytag/tagreference.html

如果display:table标签具有属性uid="foobar"(或id="foobar"),则列表的当前元素可通过页面上下文属性“foobar”获得。见http://www.displaytag.org/1.2/displaytag/tagreference.html

回答by Shlomo Georg Konwisser

Display Tag uses Implicit Objectsto store the row number. Here is an example from the Display Tag documentation:

Display Tag 使用隐式对象来存储行号。以下是 Display Tag 文档中的一个示例:

<display table id="row" name="mylist">
  <display:column title="row number" >
    <c:out value="${row_rowNum}"/>
  </display:column>
  <display:column title="name" >
    <c:out value="${row.first_name}"/>
    <c:out value="${row.last_name}"/>
  </display:column>
</display:table>

So you can use EL to access the row number which is stored as an automatically created object. Its name is defined by your idvariable (in this case row) with appended _rowNum(in this case resulting in ${row_rowNum}).

因此,您可以使用 EL 访问作为自动创建对象存储的行号。它的名称由您的id变量(在本例中为row)并附加_rowNum(在本例中为${row_rowNum})定义。

Be aware that the numbering starts at 1, not at 0.

请注意,编号从 1 开始,而不是从 0 开始。