Spring表单提交以删除集合项
我有一个页面,它有一个表单支持对象,其中包含我在页面上循环访问的集合属性。我想为用户提供通过单击删除按钮来删除集合中的项目的选项。但是我不知道在控制器中按下了哪个删除按钮,因为集合中的每个项目都有一个删除按钮。基本上我需要知道按下了哪个删除按钮,这样我就知道应该删除哪个问题 ID。解决这个问题的最佳方法是什么?请参阅我的页面如下:
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>
<sf:form method="POST" modelAttribute="surveyInfo" >
<table id="glossarysearchtable-full" border="0" cellpadding="0" cellspacing="0">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Schedule Number ${surveyInfo.surveyNumSch}
<input type="submit" class="small-short inner2" value="Save" alt="Save" title="Save" />
<input type="button" class="small-short inner2" value="Print" alt="Print" title="Print" />
<input type="button" class="small-short inner2" value="Remove" alt="Remove" title="Remove" />
<sf:input type="hidden" path="id" id="id" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyTitle" id="surveyTitle" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyName" id="surveyName" cssClass="inputbox-survey" maxlength="100" size="100" />
</tr>
<c:forEach items="${surveyInfo.allSurveyQuestions}" var="surveyQuestion" varStatus="status">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Question ${status.count} <input type="submit" class="small-short inner2" value="Remove${status.count}" alt="Remove" title="Remove" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].questionId" id="questionId${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].id" id="id${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
</td>
</tr>
<tr class="altrow" align="left">
<td height="20">Text:</td>
<td><sf:input path="allSurveyQuestions[${status.index}].questionText" id="questionText${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" /></td>
</tr>
</c:forEach>
</table>
</sf:form>
I have a page that has a form backing object that contains a collections property that I loop through on the page. I want to give the user the option of deleting an item of the collection by clicking on a remove button. However I don't know which remove button was pressed in the controller since each item in the collection has a remove button. Basically I need to know which remove button was pressed so I know what question id should be deleted. What is the best way to go about this? Please see my page below:
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>
<sf:form method="POST" modelAttribute="surveyInfo" >
<table id="glossarysearchtable-full" border="0" cellpadding="0" cellspacing="0">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Schedule Number ${surveyInfo.surveyNumSch}
<input type="submit" class="small-short inner2" value="Save" alt="Save" title="Save" />
<input type="button" class="small-short inner2" value="Print" alt="Print" title="Print" />
<input type="button" class="small-short inner2" value="Remove" alt="Remove" title="Remove" />
<sf:input type="hidden" path="id" id="id" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyTitle" id="surveyTitle" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyName" id="surveyName" cssClass="inputbox-survey" maxlength="100" size="100" />
</tr>
<c:forEach items="${surveyInfo.allSurveyQuestions}" var="surveyQuestion" varStatus="status">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Question ${status.count} <input type="submit" class="small-short inner2" value="Remove${status.count}" alt="Remove" title="Remove" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].questionId" id="questionId${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].id" id="id${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
</td>
</tr>
<tr class="altrow" align="left">
<td height="20">Text:</td>
<td><sf:input path="allSurveyQuestions[${status.index}].questionText" id="questionText${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" /></td>
</tr>
</c:forEach>
</table>
</sf:form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我解决了我的问题,我所做的是在我的域对象(也是我的表单支持对象)中创建了一个名为 isDeleted 的属性,并为页面上的每个记录创建了一个绑定复选框。如果用户单击一个或多个复选框并单击“保存”,控制器将检查该复选框的值并填充 2 个不同的集合,一个用于 toBeSaved,另一个用于 toBeDeleted。未检查的记录将被保存,检查的记录将从数据库中删除。
我在网上看到了很多通过 jquery 路由来执行此操作的选项,但我仍在学习 jquery 并且更喜欢在服务器端执行此操作。
Well I solved my issue, what I did was created a property called isDeleted in my domain object which is also my form backing object and created a bound checkbox for each record on the page. If the user clicks on the checkbox or checkboxes and clicks saves, the controller checks the value of the checkbox and populates 2 different collections one for toBeSaved and another for toBeDeleted. Unchecked records gets saved and checked records get deleted from the database.
I saw lots of options on the web to do this via the jquery route, but I am still learning jquery and preferred to do it server side.