2012-12-28 1 views
0

가변 개수의 열로 눈금을 만들고 있습니다. 나는 거의 끝났지 만 한 가지만 남았습니다 ... 나는 액세스 속성 Address (PersonProperties 인터페이스)를 사용할 수 없습니다. 나는 @Path를 올바르게 쓰는 법을 모른다. 누구든지 어떤 생각이 든다면 조언 해주세요. 다음 [ { "FirstName": "John", "LastName": "Doe", "Age": 23, "Details": [ { "Address": "Apt R113", "City": "Boston", "ZipCode": "30523" }, { "Address": "ABC 22", "City": "Paris", "ZipCode": "51112" } ] } ]GWT/GXT, JSON, @Path annotation with arrays

및 PropertyAccess 인터페이스 :

나는이 JSON이

public interface PersonProperties extends PropertyAccess<PersonDTO> { 

ModelKeyProvider<PersonDTO> key(); 

ValueProvider<PersonDTO, String> FirstName(); 

ValueProvider<PersonDTO, String> LastName(); 

ValueProvider<PersonDTO, Integer> Age(); 

@Path("Details???Address") 
ValueProvider<PersonDTO, String> Address(); 

}

+0

AutoBeans를 사용하여 JSON을 객체 그래프에 마샬링하고 있습니까? 게시 한 내용을 토대로 그 단계가 누락 된 것처럼 보입니다. –

답변

2

당신의 PersonDTO 개체를 가정하면 JSON의 객체 표현입니다, 난 당신이 인터페이스가 있다고 가정 할 다음과 같이 보이는 (AutoBeans 사용을위한) 모델 :

// I'm leaving the setters out for brevity 
public interface PersonDTO { 
    @PropertyName(value="FirstName") 
    String getFirstName(); 
    @PropertyName(value="LastName") 
    String getLastName(); 
    @PropertyName(value="Age") 
    Integer getAge(); 
    @PropertyName(value="Details") 
    List<Details> getDetails(); 
} 

public interface Details { 
    @PropertyName(value="Address") 
    String getAddress(); 
    @PropertyName(value="City") 
    String getCity(); 
    @PropertyName(value="ZipCode") 
    String getZipCode(); 
} 

사용하고있는 인터페이스 모델에이 맵핑이 있다고 가정하면 질문에 답하기 위해 @Path 주석을 사용하여 객체 속성 이름을 지정하여 JSON 이름이 아닌 속성에 액세스합니다. 따라서 단일 속성의 경우 PropertyAccess 값이 속성 이름과 같지 않은 경우 경로 주석을 사용할 수 있습니다. 이 같은 것을 사용할 수 있도록 예를 들어, 귀하의 PersonProperties 속성이, 대문자 : 당신의 세부 사항은 다음 당신이 쓴 것과 비슷한 표기법을 사용할 수 있습니다 단지 하나의 목적이었다 객체 경우

@Path("firstName") 
ValueProvider<PersonDTO, String> FirstName(); 

을합니다 (@Path 주석이 기억 게터를 지정 implicity) 객체 심에서 속성에 액세스하는 데 사용할 :

@Path("details.address") 
ValueProvider<PersonDTO, String> address(); 
// would return the address if Details was a single object 

그러나 세부 값이 JSON의 예로서 조금 다른있는 Details 값이 실제로 Details의 모음을 나타냅니다. 따라서 모든 PersonDTO 객체에 대해 여러 개의 Details 객체가 있기 때문에 모눈은 Details를 표시하는 방법을 알 수 없습니다. 그러나 나는 그것이 당신에게 이미 명확하다는 것을 짐작하고있다 그래서 특정 조건이 적용될 때 당신이 주어진 줄에있는 주소를 표시하는 것을 시도하고 있다는 것을 추측하자. 이와 같은 경우에는 자체 ValueProvider를 구현할 수 있습니다. 그런 다음

public class AddressByCityValueProvider implements ValueProvider<PersonDTO, String> { 
    public final String cityKey; 

    public AddressByCityValueProvider(String specifiedKey) { 
    this.cityKey = specifiedKey; 
    } 

    // we will display their Address if the city is Boston 
    @Override 
    public String getValue(PersonDTO person) { 
    if (null != person.getDetails()) { 
     for (Details detail : person.getDetails()) { 
     if (detail.getCity().equalsIgnoreCase(cityKey)) { 
      return detail.getAddress(); 
     } 
     } 
    } 
    return ""; // no address for specified city in object, return a blank String 
    } 

    @Override 
    public String getPath() { 
    return key; 
    } 

당신은 당신이 당신의 자신의 ValueProvider을 대체하고 사용할 수있는 키를 지정됩니다 PropertyAccess 클래스에서 ValueProvider을 사용하고 예를 들어 (내가 생각 엽차 예에서 dervied).

ColumnConfig<PersonDTO, String> addressColumn = 
    new ColumConfig<PersonDTO, String>(new AddressByCityValueProvider("Boston"), 100, "Address"); 

는 분명히 이것은 단지 하나의 예입니다 만 잘하면 당신은 아이디어를 얻을 : 도시 보스턴 때 열 구성의 경우, 거리 주소를 반환이 ValueProvider을 사용할 수 있습니다.

+0

나는 생각을 가지고 있다고 생각한다. 네 충고에 고마워, 고마워! – ladinho10