在启用 Spring 的 Velocity 模板内绑定变量集合

发布于 2024-11-29 17:00:12 字数 1001 浏览 1 评论 0原文

假设我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2024-12-06 17:00:12

我不确定答案是否清楚。试图解释
Spring 速度绑定需要整个路径作为 #springBind 的参数
因此,如果您有一个集合 dataItems,则不能像这样绑定

    #foreach($dataItem in $dataItems)
      #springFormSingleSelect( "dataItem.data1" $labelKeys "")
    #end

,而是

    #set ($end = $dataItems.size() - 1)
    #foreach($i in [0..$end])
      #springFormSingleSelect( "dataItems[$i].data1" $labelKeys "")
    #end

绑定集合项。

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

    #foreach($dataItem in $dataItems)
      #springFormSingleSelect( "dataItem.data1" $labelKeys "")
    #end

Instead

    #set ($end = $dataItems.size() - 1)
    #foreach($i in [0..$end])
      #springFormSingleSelect( "dataItems[$i].data1" $labelKeys "")
    #end

This would bind the collection items.

ゞ花落谁相伴 2024-12-06 17:00:12

怎么样:

#foreach($i in [1..4])
    #set($field = "boData.data" + $i)
    #springFormSingleSelect($field $labelKeys "")
#end

How about:

#foreach($i in [1..4])
    #set($field = "boData.data" + $i)
    #springFormSingleSelect($field $labelKeys "")
#end
生生漫 2024-12-06 17:00:12

不确定我是否理解这个问题。
但似乎您所需要的只是一个 foreach 循环。
在 Velocity 中,您也可以输入 java,如果这能让您的生活更轻松的话。

但是,您可以执行以下操作:

#set ($map = $myobject.getMap() )
#foreach ($mapEntry in $map.entrySet())
     // $mapEntry.key
     // $mapEntry.value
    #springFormSingleSelect( "boData.data1" $mapEntry.key "")
#end

您可以对列表执行相同的操作。

我希望这有帮助。问候,

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:

#set ($map = $myobject.getMap() )
#foreach ($mapEntry in $map.entrySet())
     // $mapEntry.key
     // $mapEntry.value
    #springFormSingleSelect( "boData.data1" $mapEntry.key "")
#end

You can do the same with a list.

I hope this helps. Regards,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文