1

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.

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.

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

My jsp structure and what I am currently trying is :

<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 hidden field in the display tag table is not getting populated with the "rowId" value which is a part of "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.

Please advise.

Thanks!!

Community
  • 1
  • 1
kanishk
  • 713
  • 4
  • 15
  • 31
  • i am not sure how display tag works but if the list is getting iterated than element should be on top of value stack – Umesh Awasthi Dec 14 '11 at 12:59
  • Okz, still thanks for your attention. – kanishk Dec 14 '11 at 18:36
  • @kanishk Hi, I too facing the same problem, Can you please tell me how can I over come this. here my link thanking you very much in advance http://stackoverflow.com/questions/20812145/how-to-pass-id-value-to-perform-edit-operation-in-struts2-with-displaytags – Prabha Dec 28 '13 at 07:57
  • The answer below should be helpful. – kanishk Dec 28 '13 at 09:41

2 Answers2

1

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

<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 id variable (in this case row) with appended _rowNum (in this case resulting in ${row_rowNum}).

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

1

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So do you mean that if I specify uid in the table like this `uid=foobar`, then I can use it like this --> ``. I am gonna try it out now. Thanks!! – kanishk Dec 14 '11 at 18:32
  • Hi, I tried what I mentioned above and some other stuff too but nothing happened. what am I doing wrong ? – kanishk Dec 14 '11 at 19:03
  • I don't know Struts2 very well, so I don't know what %{foobar.rowId} does exactly. The standard EL expression would be ${foobar.rowId}. – JB Nizet Dec 14 '11 at 19:06
  • If I use %{property} in an "iterator with value 'list'" it would give me the value of 'property' for current object of the list. Using ${} gives an exception "According to TLD or attribute directive in tag file, attribute value does not accept any expressions". So I guess struts works a little differently than display tag. – kanishk Dec 14 '11 at 19:13
  • So is there another way through which I can communicate to the action class that 'edit' link of which row has been clicked. I have a column named rowId in the list I present to display tag. Row Id is unique. So I basically need the particular rowId in action classs. The link you provided mentions `uid_rowNum` can that somehow be used. – kanishk Dec 14 '11 at 19:18
  • 1
    ${} is not specific to displaytag. It's the standard JSP EL. According to http://struts.apache.org/2.0.14/docs/ognl-basics.html, you need to use #attr['foobar'].rowId or #attr.foobar.rowId – JB Nizet Dec 14 '11 at 19:20
  • Now I tried %{#attr['foobar'].rowId} , %{#attr.foobar.rowId}, ${#attr['foobar'].rowId}, ${#attr.foobar.rowId}, none of them works. $ gives error. % gives no value. Is there a difference in the way struts elements and normal jsp elements communicate with the value stack (although I dont think its that way). – kanishk Dec 14 '11 at 19:37
  • 1
    The following page shows an example: http://old.nabble.com/Struts-2---DisplayTag-example-td17694341.html. value="%{#attr.foobar.rowId}" should work. Are you sure you have a getRowId() getter in the beans contained in the list? – JB Nizet Dec 14 '11 at 20:02
  • Thanks its working now. `value="%{#attr.foobar.rowId}"` worked finally. Thanks a lot. – kanishk Dec 15 '11 at 16:27
  • Hi, JB. I am facing a new problem now. I have edited my post (updated code as well). Can you please help me out. Thanks!! – kanishk Dec 19 '11 at 11:32
  • Don't edit a question once it has been answered and the answer has been accepted. Ask another separate question, and I'll try answering it. I rollbacked the edits you made. Remember that this question (and answer) might be useful to other developers. – JB Nizet Dec 19 '11 at 11:36
  • Okz. I will post another question – kanishk Dec 19 '11 at 12:01