Spring Form提交以删除集合项

时间:2012-01-29 04:21:34

标签: java forms spring-mvc controllers

我有一个页面,它有一个表单支持对象,其中包含我在页面上循环的collections属性。我想通过单击删除按钮为用户提供删除集合项的选项。但是我不知道在控制器中按下了哪个删除按钮,因为集合中的每个项目都有一个删除按钮。基本上我需要知道哪个删除按钮被按下,所以我知道应删除哪个问题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>

1 个答案:

答案 0 :(得分:1)

我解决了我的问题,我所做的是在我的域对象中创建了一个名为isDeleted的属性,该属性也是我的表单支持对象,并为页面上的每个记录创建了一个绑定复选框。如果用户单击复选框或复选框并单击保存,则控制器将检查复选框的值,并为toBeSaved填充2个不同的集合,为toBeDeleted填充另一个集合。保存未经检查的记录,并从数据库中删除已检查的记录。

我在网上看到很多选项通过jquery路由来完成,但我仍然在学习jquery并且更喜欢在服务器端做这个。