Java Reference
In-Depth Information
Figure 9.4: Showing attachments with delete buttons
Here is the code that displays the attachments in the page:
<s:hidden id="deleteIndex" name="deleteIndex"/>
<c:forEach items="${actionBean.message.attachments}"
var="attach" varStatus="loop">
<s:image name="deleteAttachment" src="/images/delete.gif"
onclick="getElementById('deleteIndex').value=${loop.index}"
style="border: none; vertical-align: bottom"/>
${attach.fileName} (${attach.size} bytes)
<br/>
</c:forEach>
Notice that we have a variable number of attachments, but every delete
image button is bound to the same
deleteAttachment
( ) event handler.
We need a parameter to indicate which attachment to delete, so we've
added a
deleteIndex
hidden input. This parameter is set to the attach-
ment index in the
onclick=
attribute. The code in the action bean deletes
the attachment from the email, and the DAO takes care of deleting the
file from the disk.
4
public
Resolution deleteAttachment()
throws
Exception {
Attachment attachment =
getMessage().getAttachments().remove(deleteIndex);
attachmentDao.delete(attachment.getId());
return new
ForwardResolution(COMPOSE);
}
4.
Deleting items by index is fine here because we don't have more than one user com-
posing the same email message. However, if you're modifying data that can be accessed
simultaneously by multiple users, it's dangerous to use list indices because deleting an
item changes the indices of all subsequence items. Use map keys instead, because delet-
ing an entry does not affect the other keys.
Search WWH ::
Custom Search