OgnlContext对象
// OgnlContext用法
publicclass OgnlDemo1 {
/**
* 1. Ognl表达式语言语言取值,取非根元素的值,必须用#号
* @throws Exception
*/
@Test
publicvoid testOgnl() throws Exception {
// 创建一个Ognl上下文对象
OgnlContextcontext = new OgnlContext();
// 放入数据
User user = new User();
user.setId(100);
user.setName("Jack");
// 【往非根元素放入数据,取值的时候表达式要用"#"】
context.put("user", user);
// 获取数据(map)
// 先构建一个Ognl表达式, 再解析表达式
Object ognl = Ognl.parseExpression("#user.name");
Object value = Ognl.getValue(ognl, context,context.getRoot());
System.out.println(value);
}
/**
* 2. Ognl表达式语言语言取值,取根元素的值,不用带#号
* @throws Exception
*/
@Test
publicvoid testOgn2() throws Exception {
// 创建一个Ognl上下文对象
OgnlContextcontext = new OgnlContext();
// 放入数据
User user = new User();
user.setId(100);
user.setName("Jack");
// 【往根元素放入数据】
context.setRoot(user);
// 获取数据(map)
// 先构建一个Ognl表达式, 再解析表达式
Object ognl = Ognl.parseExpression("name");
Object value = Ognl.getValue(ognl, context,context.getRoot());
System.out.println(value);
}
/**
* 3.Ognl对静态方法调用的支持
* @throws Exception
*/
@Test
publicvoid testOgn3() throws Exception {
// 创建一个Ognl上下文对象
OgnlContextcontext = new OgnlContext();
// Ognl表单式语言,调用类的静态方法
//Object ognl =Ognl.parseExpression("@Math@floor(10.9)");
// 由于Math类在开发中比较常用,所以也可以这样写
Object ognl = Ognl.parseExpression("@@floor(10.9)");
Object value = Ognl.getValue(ognl, context,context.getRoot());
System.out.println(value);
}
}
注意:根元素的取值不需要带# ,而非根元素需要带#
数据的迭代
Action:
class User{ private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class OgnlAction extends ActionSupport { @Override public String execute() throws Exception { List<User> list = new ArrayList<User>(); Map<Integer, Object> map = new HashMap<Integer, Object>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setId(i); user.setName("hahah" + i); list.add(user); map.put(i, user.getName()); } ActionContext.getContext().getContextMap().put("list", list); ActionContext.getContext().getSession().put("map", map); return "ognl"; } } JSP:
<body> 1、list迭代<br/> <table> <tr> <td>编号</td> <td>姓名</td> </tr> <s:iterator var="user" value="#request.list" status="vs"> <tr class=<s:property value="#vs.even?'even':'odd'"/>> <td><s:property value="#user.id"/></td> <td><s:property value="#user.name"/></td> </tr> </s:iterator> </table> <br/> 2、map迭代<br/> <table> <tr> <td>编号</td> <td>姓名</td> </tr> <s:iterator var="user1" value="#session.map" status="vs1"> <tr> <td><s:property value="#user1.key"/></td> <td><s:property value="#user1.value"/></td> </tr> </s:iterator> </table> <br/> 3、构建List集合 <br/> <s:iterator var="str" value="{'a','b','c'}"> <s:property value="#str"/> </s:iterator> <br/> 4、构建map集合<br/> <s:iterator var="str1" value="#{'cn':'china','en':'english' }"> <s:property value="#str1.key"/> <s:property value="#str1.value"/><br/> </s:iterator> </body>
打印结果:
1、list迭代
编号姓名0hahah01hahah12hahah23hahah34hahah45hahah56hahah67hahah78hahah89hahah9 2、map迭代 编号姓名0hahah01hahah12hahah23hahah34hahah45hahah56hahah67hahah78hahah89hahah9 3、构建List集合 abc 4、构建map集合 cnchina enenglish