4.3.2 프로퍼티,배열,목록,지도 및 인덱서 by ys
속성 참조를 탐색하는 것은 쉽습니다. 이렇게하려면 중첩 된 속성 값을 나타내는 마침표를 사용하십시오. Inventor
클래스인 pupin
및tesla
의 인스턴스 예제 섹션에 사용 된 클래스에 나열된 데이터 채워졌습니다 . 테슬라의 출생 연도와 푸핀의 출생 도시를 "아래로"탐색하기 위해 다음과 같은 표현을 사용합니다.
// evals to 1856
int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
속성 이름의 첫 글자는 대소 문자를 구분하지 않습니다. 배열 및 목록의 내용은 다음 예제와 같이 대괄호 표기법을 사용하여 가져옵니다.
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
// Inventions Array
// evaluates to "Induction motor"
String invention = parser.parseExpression("inventions[3]").getValue(
context, tesla, String.class);
// Members List
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("Members[0].Name").getValue(
context, ieee, String.class);
// List and Array navigation
// evaluates to "Wireless communication"
String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(
context, ieee, String.class);
지도의 내용은 괄호 안에 리터럴 키 값을 지정하여 얻습니다. 다음 예제에서는 Officers
맵의 키 가 문자열 이기 때문에 문자열 리터럴을 지정할 수 있습니다.
// Officer's Dictionary
Inventor pupin = parser.parseExpression("Officers['president']").getValue(
societyContext, Inventor.class);
// evaluates to "Idvor"
String city = parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(
societyContext, String.class);
// setting values
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(
societyContext, "Croatia");
Last updated
Was this helpful?