8.2 Basic expression language features
8.2.1 Accessing bean properties
package examples.chap08;
import java.util.List;
import java.util.Set;
import java.util.Map;
public class Muppet{
public static final String OG_MUPPET = "Kermit";
public static Muppet getOgMuppet() {
Muppet og = new Muppet();
og.setName(OG_MUPPET);
return og;
}
private String name;
private int age;
private boolean lifesized;
private Muppet father;
private Muppet mother;
private List children;
private Set foods;
private Map favorites;
public int avgParentsAge() {
return ((father.getAge() + mother.getAge()) / 2);
}
// getters and setters
...
}
- Table 8.1 comparison of java code and OGNL expressions for accessing bean properties
| Java code | OGNL expression(Muppet is the root) |
|---|
| muppet.getName() | name |
| muppet.getMother().getName() | mother.name |
| muppet.getFather().getFather().getAge() | father.father.age |
8.2.2 Literals and operators
- Table 8.2 All the literals supported by OGNL
| Literal type | Example |
|---|
| char | 'a' |
| String | 'hello world' |
| "hello world" |
| Boolean | true |
| false |
| int | 123 |
| double | 123.5 |
| BigDecimal | 123b |
| BigInteger | 123h |
<ww:property value="\"a\""/>
is not the same as
<ww:property value="'a'"/>
- Table 8.3 Operators supported by OGNL
| Operation | Example |
|---|
| add ( + ) | 2 + 4 |
| 'hello' + 'world' |
| subtrct ( - ) | 5 - 3 |
| multiply ( * ) | 8 * 2 |
| divide ( / ) | 9 / 3 |
| modules (mod) | 9 mod 2 |
| increment (++) | ++foo |
| foo++ |
| decrement (--) | bar-- |
| --bar |
| equality (==) | foo == bar |
| inequality (!=) | foo != bar |
| in | foo in someList |
| not in | foo not in someList |
| assignment | foo = 123 |
8.2.3 Calling methods
- Table 8.4 Sample method and property OGNL expressions compared to their Java equivalents
| Java code | OGNL expression(Muppet is the root) |
|---|
| muppet.avgParentsAge() | avgParentsAge() |
| muppet.avgParentsAge() - muppet.getAge() | avgParentsAge() - age |
| ( muppet.getMother().getAge() + muppet.getFather().getAge() ) / 2 | (mother.age + father.age) / 2 |
| muppet.getMother().getAge() | getMother().age or mother.getAge() or mother.age |
- 보안상의 이유로 ParameterInterceptor 가 적용된 부분에서 method call이 허용되지 않는다.
8.2.4 Setting values and expression lists
father.age = 27, mother.age = 25, _avgParentsAge()
8.2.5 Accessing static methods and fields
- @[WEBSTUDY:ClassName]@[WEBSTUDY:FieldOrMethod] 표기법을 이용해서 static 메소드 또는 프로프티를 참조한다.
@examples.chap8.Muppet@OG_MUPPET
@examples.chap8.Muppet@getOgMuppet()
- 전체 패키지 이름을 쓰지 않고 접두사 vs(Value Stack)를 이용해서 사용한다.
@vs@OG_MUPPET
@vs@getOgMuppet()
- vs보다는 전체 패키지 이름을 권장(리팩토링)
8.2.6 Accessing the OGNL context and the ActionContext
- OgnlContext와 ActionContext는 같은 것이다.
- # 기호를 접근가능하다.
- Table 8.5 Examples that access the ActionContext using OGNL
| Java code | OGNL expression |
|---|
| ActionContext().getContext().getParameters() | #parameters |
| ActionContext().getContext().getParameters().size() | #parameters.size |
| ((Kermit)(ActionContext().getContext().get("kermit")).getAge() | #kermit.age |
- WebWork에서 사용되는 구분자
■ Parameters - 현재 요청에 대한 HttpservletRequest parameters 담고 있는 Map
■ Request - 현재 요청에 대한 HttpservletRequest attributes 담고 있는 Map
■ Session - 현재 세션에 대한 HttpSession attributes 담고 있는 Map
■ Application - 현재 어플리케이션에 대한 ServletConfig attributes 담고 있는 Map
■ Attr - 요청(request), 세션(session)과 어플리케이션 맵으로부터 attributes 찾는데 사용되는 특수한 맵