问题描述
我使用Spring MVC 3.0和JSP。我有一个对象:
public class ObjectWrapper {
private List< SomeTO> someTOs;
SomeTO
包含字段如名称
和 id
。我如何创建一个用户可以动态添加到 SomeTO
列表的表单?我搜索了它并发现了一些关于 spring:bind
的内容,但对我来说还不清楚。
在表单支持方法中,将列表设置为属于apache commons集合库的LazyList。
Factory factoryFactory = new Factory(){
public Object create(){
SomeTO rtVl = new SomeTO();
return rtVl;
}
};
myFormBacking.setSomeTOs(LazyList.decorate(myFormBacking.getSomeTOs));
然后在表单上,当您将数据发送到服务器时,您可以像这样做 p>
< input name =someTOs [0] .namevalue =/>
如果您使用
<%@ taglib prefix =formuri =http://www.springframework.org/tags/form%>
然后您可以简单地去。
< form:input path =someTOs [0] .name/>
在将数据发布到服务器之前,为了简化修剪,请设置集合中元素的数量。因此,如果用户添加了5个TO,那么将该长度值以表单发送。
现在在服务器上,您必须在保存之前修剪列表。这里是修剪功能
public List< SomeTOs> pruneList(List List< SomeTOs> rtVl = new ArrayList< SomeTOs>();
for(int i = 0; i< unpruned.length&&expectedLength; ++ i){
rtVl.add(unpruned.get(i);
}
return rtVl;
}
这里使用了prune函数提交(保存前)
wrapper.setSomeTOs(pruneList(wrapper.getSomeTOs(),Integer.parseInt(request.getParameter( expectedLength)));
I use Spring MVC 3.0 and JSP. I have an object:
public class ObjectWrapper {
private List<SomeTO> someTOs;
}
Class SomeTO
contains fields like name
and id
. How can I create a form on which an user can dynamically add to list of SomeTO
? I googled it and found something about spring:bind
, but it's unclear for me.
In the form backing method, set the list to a LazyList which is part of the apache commons collection library.
Factory notificationFactory = new Factory() {
public Object create() {
SomeTO rtVl = new SomeTO();
return rtVl;
}
};
myFormBacking.setSomeTOs(LazyList.decorate(myFormBacking.getSomeTOs));
Then on your form, when you send the data to the server you can do it like this
<input name="someTOs[0].name" value="" />
And if your using
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
then you can simply go.
<form:input path="someTOs[0].name" />
Before posting the data to the server, to make pruning easier, set the number of elements in the collection. So if the user added 5 TOs, then send that length value in the form post.
ON the server now, you must prune the list before you save it. Here is the function for pruning
public List<SomeTOs> pruneList(List<SomeTOs> unpruned,int expectedLength){
List<SomeTOs> rtVl = new ArrayList<SomeTOs>();
for (int i = 0; i < unpruned.length && expectedLength; ++i){
rtVl.add(unpruned.get(i);
}
return rtVl;
}
Here is the use of the prune function in the on submit (before save)
wrapper.setSomeTOs(pruneList(wrapper.getSomeTOs(),Integer.parseInt(request.getParameter("expectedLength)));
这篇关于Spring MVC jsp对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!