8.3 Working with collections

8.3.1 Working with lists and arrays

  • OGNL에서 lists와 arrays는 같이 간주된다.
  • Table 8.6 Examples of array and list notation
java codeOGNL expression
list.get(0)list[WEBSTUDY:0]
array[WEBSTUDY:0]array[WEBSTUDY:0]
((Muppet)list.get(0)).getName()list[WEBSTUDY:0].name
array.lengtharray.length
list.size()list.size
list.isEmpty()list.isEmpty
  • Table 8.7 Examples of constructing lists in the simplified notation
java codeOGNL expression
List list = new ArrayList(3){1, 3, 5}
list.add(new Integer(1));
list.add(new Integer(3));
list.add(new Integer(5));
return list;
List list = new ArrayList(2){"foo", "bar"}[WEBSTUDY:1]
list.add("foo");
list.add("bar");
return list.get(1);

8.3.2 Working with maps

  • OGNL은 autoboxing을 한다.
  • Table 8.8 Examples of map notation
java codeOGNL expression
map.get("foo")map[WEBSTUDY:'foo']
map.get(new Integer(1));map[WEBSTUDY:1]
Muppet muppet = (Muppet)map.get("Kermit");map[WEBSTUDY:'kermit'].age
return muppet.getAge()
map.put("foo", "bar")map[WEBSTUDY:'foo'] = 'bar'
map.size()map.size
map.isEmpty()map.isEmpty
  • Table 8.9 Creating maps dynamically
java codeOGNL expression
Map map = new HashMap(2);
map.put("foo", "bar");
map.put("baz", "whazzit");
return map;
#{"foo" : "bar",
"baz" : 'whazzit" }
Map map = new HashMap(3);
map.put(new Integer(1), "one");
map.put(new Integer(2), "two");
map.put(new Integer(3), "three");
return map;
#{1 : "one",
"2" : 'two",
"3" : 'three" }
Map map = new HashMap(2);
map.put(kermit.getName(), kermit.getMother().getName());
map.put(oscar.getName(), oscar.getMother.getName());
return map;
#{ #kermit.name : #kermit.mother.name,
#oscar.name : #oscar.mother.name }
ActionContext.getContext().getParameters().get("id")#parameters[WEBSTUDY:'id']
String name = muppet.getName();
Map map = ActionContext.getContext().getSession();
return map.get("muppet-"+name);
#session[WEBSTUDY:'muppet-'+ name]
session.put("muppet-kermit", muppet);#session[WEBSTUDY:'muppet-kermit'] = muppet
  • Table 8.10 Shorthand notation for accessing map values
Array-style notationShorthand notation
#parameters[WEBSTUDY:'id']#parameters.id
#request[WEBSTUDY:'id']#request.id
#application[WEBSTUDY:'config']#application.config

8.3.3 Filtering and projectiong collections

  • OGNL 은 필터링과 프로젝션의 두가지 특징이 있다.
  • 필터링 - 크기가 N인 collection의 부분집합을 리턴한다.
    collection.{? expression }
    foo.{^ bar} 첫번째 매칭
    foo.{$ bar} 마지막 매칭
  • 프로젝션 - 프로젝션 룰에 따라 데이터를 변화시킨다.
    collection.{ expression }
  • Table 8.11 Examples of filtering and projection
OGNL expression설명
children.{name}모든 children의 이름들을 가지고 온다. children collection(Muppets)에서 names collection(Strings)로 데이터 변화
children.{?#this.age > 2}2년 이상의 children을 필터한다. #this 평가되어지는 객체의 구분하는 특수한 변수
children.{?#this.age <= 2}.{name}2년 이하의 필터된 children의 이름들을 프로젝트한다
children.{name + ' -> ' + mother.name}name -> motherName 의 형태를 가지는 스트링의 리스트를 프로젝트한다(?)

8.3.4 The multiple uses of "#"

  • # 연산자의 3가지 사용법
    ■ ActionContext에서 값들을 참조하기(8.2.6)
    ■ Maps를 생성하기(8.3.2)
    ■ collection을 필터링하거나 프로젝션하기(8.3.3)