从jsp上的显示标记表获取行ID到struts 2动作类

时间:2011-12-14 11:53:19

标签: javascript jsp struts2 action displaytag

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

stackoverflow上有一些关于相同的帖子([问题]:How to use multiple buttons (one on each line) for JSP page using Struts2,[问题]:Get value from a row in a JSP page using display tag,[问题]:Retrieving Value from Row in Struts2 Table While using Displaytag),但我找不到解决方案对我有用。

google给了我(http://demo.displaytag.org/displaytag-examples-1.2/example-decorator-link.jsp),但它使用了我不想使用的URL重写,而且演示了使用struts(我使用的是struts 2)。

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

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

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

请告知。

谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

显示标记使用Implicit Objects存储行号。以下是显示标签文档中的示例:

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

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

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

相关问题