2014-10-18 4 views
0

일시적인 List 속성에 값을 설정 (또는 바인드)하려고합니다. 그러나 나는 수집 물에 실패했다. 한편 setter에서 일시적인 String 특성이 잘 작동합니다.Grails 도메인 클래스의 일시적인 컬렉션 속성 설정 자 문제

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 = { 
    } 
} 

답변

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 = { 
    } 
} 
+0

주셔서 감사합니다 approche 내가 당신만큼 유사 해결했다. – daimon