Spring Webflow-从列表中删除项目? [英] Spring Webflow - deleting an item from a list?

查看:76
本文介绍了Spring Webflow-从列表中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Webflow 2.3.2,并且在一个步骤中,用户可以在绑定的模型对象中的列表中添加/删除(他们在修改后仅返回到当前步骤).例如,我的对象可能看起来像这样:

I'm using Webflow 2.3.2 in an application, and on one step the user can add/delete from a list in the bound model object (they simply return to the current step after the modification). For example my object might look like this:

public class MyInfo implements Serializable {
    List<String> myList = new ArrayList<String>();
}

在webflow中执行添加"没问题,因为我只是将新对象粘贴在列表的末尾,但是对于删除",我需要标识要删除的元素.我现在正在使用预定义的EL对象"currentEvent",并获取原始事件值",该值已用要删除的记录ID填充.我想知道是否有更优雅的方法可以做到这一点,因为这似乎需要很长的路要走. 有人可以建议一种更好的方法吗?以下是我目前正在做的事情的说明:

Doing the "add" in webflow is no problem because I simply stick the new object on the end of the list, but for the "delete" I need to identify the element to delete. What I'm doing now is using the "currentEvent" predefined EL object and grabbing the raw event "value" which I've populated with the ID of the record to delete. I'm wondering if there's a more elegant way to do this, because this seems like going the long way around. Can anyone suggest a better way of doing this? Here's an illustration of what I'm doing now:

我的JSP文件(请注意删除"按钮):

My JSP file (note the "delete" button):

<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"文件:

My "flow.xml" file:

<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;
}

推荐答案

似乎主要问题是如何使用单个<button type='submit'/>元素同时提交_eventId和帐户索引?

It seems like the main issue is how to submit both the _eventId and account index using a single <button type='submit'/> element?

这是一个很好的问题&有关此问题的答案:如何克服html表单嵌套限制?.将隐藏的输入与帐户索引一起使用无济于事,因为提交"按钮仍然会提交所有隐藏的输入.

Here is a great question & answers about exactly this issue: How do you overcome the html form nesting limitation?. Using a hidden input with the account index doesn't help because the submit button would submit all of the hidden inputs anyway.

当然,只要您不想提交其他任何内容(例如,

Of course, you could use an anchor instead of a submit button as long as you didn't want to submit anything else, e.g.

<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请求.无论如何,我还是倾向于使用锚,因为这似乎是最不容易破解的选择.但是,阅读完链接的问题后,我发现有HTML5替代方法- "formaction"属性.在这种情况下,JSP将如下所示:

but semantically, a "delete" operation shouldn't be an HTTP GET request. I tend to use an anchor anyway, because it seems like the least hacky alternative. But, after reading through the linked question, I see that there is an HTML5 alternative - the "formaction" attribute. In this case, the JSP would look something like this:

<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参数.

Basically, the selected button overrides the enclosing form's 'action' attribute, to include the _eventId parameter.

您需要考虑对浏览器的支持,但是也许您可以提供JavaScript polyfill以支持较旧的浏览器.

You'd need to consider the browser support, but maybe you can provide a javascript polyfill to support older browsers.

由于从列表中选择一项(出于删除,显示等目的)是一个非常常见的用例,因此我喜欢使用自定义列表类,该类允许您将索引绑定到列表本身:

Since selecting an item from a list (for the purposes of deleting it, displaying it, whatever) is a very common use-case, I like to use a custom list class which allows you to bind the index to the list itself:

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);
    }
}

然后,您可以将所选索引直接绑定到列表,并在绑定发生后调用list.getSelectedItem().

Then, you could bind the selected index directly to the list and just call list.getSelectedItem() after binding has occurred.

这篇关于Spring Webflow-从列表中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆