绑定春天启用Velocity模板内的变量集合 [英] Binding a variable collection inside a Spring enabled Velocity template
问题描述
假如我有一个速度1.5模板下面的表单支持对象:
Suppose I have the following form backing object for a Velocity 1.5 template:
public class Bucket {
String data1;
String data2;
String data3;
String data4;
// getters setters blah blah...
}
我想这四个属性的字符串绑定到字符串以下的java.util.Map,里面四个引入下来单一选择控件:
I'd like to bind these four String attributes to the following java.util.Map of Strings, inside four drop down single select controls:
"a" : "1"
"b" : "2"
"c" : "3"
"d" : "4"
如果我的网页的控制器模型里面,我命名为支持对象boData,和值映射labelKeys,速度可以绑定的支持对象的属性对我来说:
If inside my page's controller Model, I name the backing object "boData", and the value map "labelKeys", velocity can bind the backing object's properties for me:
#springFormSingleSelect( "boData.data1" $labelKeys "")
#springFormSingleSelect( "boData.data2" $labelKeys "")
#springFormSingleSelect( "boData.data3" $labelKeys "")
#springFormSingleSelect( "boData.data4" $labelKeys "")
但是,有没有办法来避免调用#springFormSingleSelect四次?我的意思是,这里有一个模式,但我看不到的方式来恩preSS它Velocity的条款。
However, is there a way to avoid invoking #springFormSingleSelect four times? I mean, there's a pattern here, but I can't see the way to express it in Velocity's terms.
如果举例来说,我反而确定这四个String类桶内属性作为Java数组或者java.util.List的,我怎么能告诉速度,我需要它来绑定一个下拉单中选择控制每个元素列表中?
If for example, I defined instead these four String attributes inside class Bucket as a Java array, or a java.util.List, how could I tell velocity that I need it to bind a drop down single select control for each element in the List?
public class Bucket {
List<String> dataItems = new ArrayList<String>();
// getter, setter...
}
我感谢你,你可以提供任何见解!
I thank you for any insight you could provide!
推荐答案
如何
#foreach($i in [1..4])
#set($field = "boData.data" + $i)
#springFormSingleSelect($field $labelKeys "")
#end
这篇关于绑定春天启用Velocity模板内的变量集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!