我在一个应用程序中使用 Webflow 2.3.2,并且用户可以在绑定(bind)模型对象的列表中添加/删除一个步骤(他们只是在修改后返回到当前步骤)。例如我的对象可能看起来像这样:
public class MyInfo implements Serializable {
List<String> myList = new ArrayList<String>();
}
在 webflow 中执行“添加”没有问题,因为我只是将新对象粘贴到列表的末尾,但对于“删除”,我需要识别要删除的元素。我现在正在做的是使用“currentEvent”预定义 EL 对象并获取原始事件“值”,我用要删除的记录 ID 填充了它。我想知道是否有更优雅的方法来做到这一点,因为这似乎要走很长的路要走。 任何人都可以建议更好的方法吗?这是我现在正在做的一个例子:
我的 JSP 文件(注意“删除”按钮):
<c:forEach items="${myInfo.myList}" var="listItem" varStatus="listItemStatus">
<c:set var="v" value="${listItemStatus.index}"/>
<div><form:input id="listItemValue_${v}" path="myInfo.myList[${v}]"/></div>
<div><button id="deleteItem_${v}" name="_eventId_deleteItem" type="submit" value="${v}">Delete This Item</button></div>
</c:forEach>
我的“flow.xml”文件:
<transition on="deleteItem" bind="false" validate="false">
<evaluate expression="flowService.deleteItem(flowScope.myInfo, currentEvent.attributes)" result="flowScope.myInfo" />
</transition>
我的事件处理程序:
public MyInfo deleteAccount(MyInfo myInfo, LocalAttributeMap currentEvent) {
myInfo.getMyList().remove(Integer.valueOf((String)(currentEvent.asMap().get("_eventId_deleteItem"))).intValue());
return myInfo;
}
最佳答案
似乎主要问题是如何同时提交 _eventId
和帐户索引使用单个 <button type='submit'/>
元素?
这里有一个关于这个问题的很好的问答:How do you overcome the html form nesting limitation? .使用带有帐户索引的隐藏输入没有帮助,因为提交按钮无论如何都会提交所有隐藏的输入。
当然,只要您不想提交任何其他内容,例如,您可以使用 anchor 而不是提交按钮。
<a href="${flowExecutionUrl}&_eventId=deleteItem&index=${v}">Delete This Item<a>
和
<transition on="deleteItem" bind="false" validate="false">
<evaluate expression="flowService.deleteItem(flowScope.myInfo, requestParameters.index)" result="flowScope.myInfo"/>
</transition>
但从语义上讲,“删除”操作不应该是 HTTP GET 请求。无论如何我倾向于使用 anchor ,因为它似乎是最不老套的选择。但是,在阅读了链接的问题后,我发现有一个 HTML5 替代方案 - the "formaction" attribute .在这种情况下,JSP 看起来像这样:
<form:form action="${flowExecutionUrl}" method="post" commandName="backingObject">
<c:forEach items="${myInfo.myList}" var="listItem" varStatus="status">
<form:input path="myInfo.myList[${status.index}]"/>
<button type="submit" name="index" value="${status.index}"
formaction="${flowExecutionUrl}&_eventId=deleteItem">
Delete This Item
</button>
</c:forEach>
</form:form>
基本上,选定的按钮会覆盖封闭表单的“action”属性,以包含 _eventId
参数。
您需要考虑浏览器支持,但也许您可以提供一个 javascript polyfill 来支持旧浏览器。
旁注
由于从列表中选择一个项目(出于删除、显示等目的)是一个非常常见的用例,我喜欢使用自定义列表类,它允许您将索引绑定(bind)到列表本身:
public class SelectionList<T> extends ArrayList<T> {
// the index of the selected item within the list
private Integer index;
public SelectionList() {
}
public SelectionList(Collection<? extends T> c) {
super(c);
}
// this is used for binding to/from the view layer, and shouldn't be called by application code
public Integer getIndex() {
return index;
}
// this is used for binding to/from the view layer, and shouldn't be called by application code
public void setIndex(Integer index) {
this.index = index;
}
// use this to retrieve the selected item from the list.
public T getSelectedItem() {
if (index == null || index >= super.size() || index < 0) {
return null;
}
return super.get(index);
}
}
然后,您可以将选定的索引直接绑定(bind)到列表,只需调用 list.getSelectedItem()
绑定(bind)发生后。
关于Spring Webflow - 从列表中删除一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280072/