【问题标题】:Bind objects in a Set collection在 Set 集合中绑定对象
【发布时间】:2011-05-24 16:09:29
【问题描述】:

我有一个填充集合的一些对象的表单。我用 List 实现了集合(一切都很好),但现在我想使用 Set。我注册了一个属性编辑器来创建对象。问题是我不知道我必须使用什么语法来填充 Set 的对象。

public class MyObject(){
    Set<OtherObject> otherObjects = new HashSet();
}

我尝试过使用表单标签中的语法,比如列表的语法:

name="otherObjects['${status.index}']"

和地图的语法一样:

name="otherObjects['${id}']"

但两者都出现错误,因为“索引属性路径'otherObjects [0]'中引用的属性既不是数组也不是列表也不是地图”

我也试过

name="otherObjects"

但这不会填充对象。 谁能告诉我如何将数据绑定到 Set 中的对象。

谢谢

【问题讨论】:

    标签: java spring data-binding jsp spring-mvc


    【解决方案1】:

    根据documentation on data binding,您只能使用方括号表示法绑定到“自然有序集合”中的嵌套对象。一组不合格。

    【讨论】:

      【解决方案2】:

      你不能在表单标签中使用旧语法并添加一个将返回一组对象的getter吗?

      public class MyObject(){
        List<OtherObject> otherObjectsList = new ArrayList<OtherObjects>();
      
        public Set<OtherObject> getOtherObject()
        {
          return new HashSet<OtherObject>(otherObjectsList);
        }
      }
      

      【讨论】:

      • 我认为这个解决方案不是很干净,它只是一种避免绑定到 Set 的方法。
      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 2011-01-02
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多