【问题标题】:Grails domain class transient collection attribute setter issueGrails域类瞬态集合属性设置器问题
【发布时间】:2014-12-14 01:22:54
【问题描述】:

我正在尝试将值设置(或绑定)到临时列表属性。但我在收藏方面失败了。 另一方面,瞬态 String 属性在 setter 上运行良好。

Grails 2.4.3 版

有什么建议吗?

@Resource(uri = "/api/samples", formats = ["json"])
class Sample {

    static transients = ["fields","sample"]

    String regions
    String name
    String sample


    List<String> fields

    List<String> getFields() {
        this.regions != null ? Arrays.asList(regions.split("\\s*,\\s*")) : new ArrayList<String>();
    }

    void setFields(List<String> fields) {
        if (fields != null && !fields.isEmpty()) {
            this.regions = fields.join(",")
        }
    }

    void setSample(String sample){
        this.name = sample
    }

    static mapping = {
    }
}

【问题讨论】:

    标签: grails collections grails-orm getter-setter transient


    【解决方案1】:

    默认情况下,无类型字段是瞬态的,因此这种替代方法应该可以工作(并且更加简洁):

    @Resource(uri = "/api/samples", formats = ["json"])
    class Sample {
    
        static transients = ["sample"]
    
        String regions
        String name
        String sample
    
    
        def getFields() {
            this.regions != null ? Arrays.asList(regions.split("\\s*,\\s*")) : []
        }
    
        void setFields(def fieldList) {
            if (fieldList) {
                this.regions = fieldList.join(",")
            }
        }
    
        void setSample(String sample){
            this.name = sample
        }
    
        static mapping = {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多