在启用 Spring 的 Velocity 模板内绑定变量集合
假设我有 Velocity 1.5 模板的以下表单支持对象:
public class Bucket {
String data1;
String data2;
String data3;
String data4;
// getters setters blah blah...
}
我想将这四个字符串属性绑定到以下字符串的 java.util.Map,在四个下拉单选控件内:
"a" : "1"
"b" : "2"
"c" : "3"
"d" : "4"
如果在我的页面控制器模型内,我将支持对象命名为“boData”,将值映射命名为“labelKeys”,速度可以为我绑定支持对象的属性:
#springFormSingleSelect( "boData.data1" $labelKeys "")
#springFormSingleSelect( "boData.data2" $labelKeys "")
#springFormSingleSelect( "boData.data3" $labelKeys "")
#springFormSingleSelect( "boData.data4" $labelKeys "")
但是,有没有办法避免调用 #springFormSingleSelect 四次?我的意思是,这里有一个模式,但我看不到如何用 Velocity 的术语来表达它。
例如,如果我将 Bucket 类中的这四个 String 属性定义为 Java 数组或 java.util.List,那么我如何告诉 Velocity 我需要它为列表中的每个元素绑定一个下拉单选择控件?
public class Bucket {
List<String> dataItems = new ArrayList<String>();
// getter, setter...
}
我感谢您提供的任何见解!
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...
}
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"
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 "")
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定答案是否清楚。试图解释
Spring 速度绑定需要整个路径作为 #springBind 的参数
因此,如果您有一个集合 dataItems,则不能像这样绑定
,而是
绑定集合项。
I am not sure if the answer was clear. Trying to explain
Spring velocity binding requires the entire path as an argument to the #springBind
So If you have a collection dataItems, you cannot bind like
Instead
This would bind the collection items.
怎么样:
How about:
不确定我是否理解这个问题。
但似乎您所需要的只是一个
foreach
循环。在 Velocity 中,您也可以输入 java,如果这能让您的生活更轻松的话。
但是,您可以执行以下操作:
您可以对列表执行相同的操作。
我希望这有帮助。问候,
Not sure if I understand the question.
But seems like all you need is a
foreach
loop.In Velocity you can type java as well if that makes your life easier.
However you can do something like:
You can do the same with a list.
I hope this helps. Regards,