将索引属性从 jsp 发布到操作 class 的数组列表出现问题

Issue with posting arraylist with indexed properties from jsp to action class

我遇到了这个问题,我在隐藏输入中动态生成了值,如下所示:

<div id="items-div" class="selection-div">
    <input name="selectedItem[0].articleName" id="selectedItem" type="hidden" value="cereal"></input>
    <input name="selectedItem[0].quantity" id="selectedItem" type="hidden" value="2"></input>
    <input name="selectedItem[1].articleName" id="selectedItem" type="hidden" value="yogurt"></input>
    <input name="selectedItem[1].quantity" id="selectedItem" type="hidden" value="10"></input>
</div>

我根据用户在屏幕上的选择,使用 jquery 将这些附加到项目-div,每个项目都是一个项目。

public class Item {

    private String articleName = "";
    private int quantity = 0;

    public String getArticleName() {
        return articleName;
    }
    public void setArticleName(String articleName) {
        this.articleName = articleName;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

这是表单 class,其中包含这些项目的 ArrayList

public class CreateArticleForm extends ActionForm {

    private ArrayList<Item> selectedList = new ArrayList<Item>();

    public Item getSelectedItem(int index) {
        if (selectedList == null) {
            selectedList = new ArrayList<Item>();
        }
        while (index >= selectedList.size()) {
            selectedList.add(new Item());
        }

        return (Item) selectedList.get(index);
    }
    public ArrayList<Item> getSelectedList() {
        return selectedList;
    }
    public void setSelectedList(ArrayList<Item> selectedList) {
        this.selectedList = selectedList;
    }

}

最后是动作class

public class CreateArticleAction extends
        Action {

    public ActionForward executeAction(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response, User user)
            throws Throwable {

            CreateArticleForm articleForm = (CreateArticleForm) form;
            //... do stuff
    }

}

我希望在 post 上所有服务器都将收集所有 selectedItem[X] 并调用 getSelectedItem 来构建 arraylist 并填充它然后为每个对象设置属性但是当我提交时在调试页面上,我看到 selectedList arraylist 是空的。我使用的是基于先前分配的逻辑,其中带有索引属性的信息是在首次加载页面时生成的,然后使用 logic:iterate 标记打印,然后每个输入都有一个 indexed="true" 属性,但由于在这种情况下它来自用户在页面上单击的内容,我改为使用 jquery 来填写 "items-div" 用户选择的内容,但它应该是一样不?我错过了什么吗?它运行以前就好了...谢谢你的时间。

编辑: 如果我在 jsp 页面中对隐藏输入进行硬编码测试,则以上内容有效,它提交那些但不是动态创建并附加 jquery.

    $('.items-div').append($('<input/>').attr({ 
type : 'hidden', name: 'selectedItem['+index+'].articleName', id : 'selectedItem', value: objItem.articleName}));

我早就应该知道了,但显然这是一种安全措施,只允许回发最初呈现的元素,所以我硬编码的那些工作得很好,但任何其他附加的 -after - 渲染不会被提交,所以我可能不得不将对象放在对象数组中,JSONfiy 并将它们作为字符串发送到操作 class,使用现有的隐藏输入。