我有这个问题,我已动态生成的隐藏的输入值,就像这样:问题与JSP发布与索引属性的ArrayList到动作类
<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取决于用户在屏幕上选择的内容,每一个都是一个Item。
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;
}
}
这是具有这些项目
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;
}
}
最后动作类的ArrayList窗体类
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
}
}
我预计在后所有的服务器将圆取出所有selectedItem [X]并调用getSelectedItem来构建数组列表并填充数组,然后将属性设置为每个对象,但是当我在调试时提交页面时,我会看到selectedList arraylist co消除空虚。我正在使用这个基于前一个任务的逻辑,其中带有索引属性的信息是在第一次加载页面时生成的,然后用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}));
在您生成的代码中,页面上每个元素的ids应该是唯一的。此外,输入标签不具有单独的关闭标签。 – Bindrid
谢谢!我将删除这些ID并查看是否这是使它无法正常工作的问题 至于输入,我注意到这一点以及它给我带来了困扰,因为我使用下面的代码来追加这些输入,并且应该将它们发布为但是当我加载页面,他们看起来像那样... '$('。selection-div')。append($('').attr({' 'type:'hidden',名称:'selectedItem ['+ index +']。articleName',id:'selectedItem',value:objItem.articleName}));' – Ricardo
没有骰子,只在当前工作,如果我硬编码的输入,但不与附加jQuery ... – Ricardo