Ognl表达式语言使用以及迭代
来源:互联网 发布:那些软件 新用户 编辑:程序博客网 时间:2024/06/06 02:41
OgnlContext对象
// OgnlContext用法
publicclass OgnlDemo1 {
/**
* 1.Ognl表达式语言语言取值,取非根元素的值,必须用#号
*@throws Exception
*/
@Test
publicvoid testOgnl()throws Exception {
//创建一个Ognl上下文对象
OgnlContextcontext =newOgnlContext();
//放入数据
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 =newOgnlContext();
//放入数据
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 =newOgnlContext();
//Ognl表单式语言,调用类的静态方法
//Objectognl =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迭代
2、map迭代
编号姓名0hahah01hahah12hahah23hahah34hahah45hahah56hahah67hahah78hahah89hahah9
3、构建List集合
abc
4、构建map集合
cnchina
enenglish
- Ognl表达式语言使用以及迭代
- OGNL表达式语言以及在Structs中的运用
- OGNL表达式语言介绍
- OGNL表达式语言介绍
- OGNL表达式语言
- OGNL表达式语言介绍
- OGNL表达式语言
- ognl表达式语言
- 六、OGNL表达式语言
- OGNL表达式语言
- OGNL表达式语言学习
- OGNL表达式语言1
- OGNL表达式语言
- OGNL表达式语言
- OGNL表达式语言(一)
- 国际化、OGNL表达式语言
- OGNL表达式语言学习
- OGNL表达式语言
- 深入了解mysql数据传输编码原理
- Spring mvc批量插入sql配置
- AndFix的使用(详解)
- Qt5开发学习之模型/视图结构(十)
- 安装sap gui 750后,打开smartforms提示:CSapEditorCtrl::GetObject: Object 13 does not exist的解决方法
- Ognl表达式语言使用以及迭代
- 实例抓包分析USB鼠标枚举数据流
- p的自动换行
- linux内核空间和用户空间的是怎样区别的,如何交互,如何从用户空间进入内核空间
- 【java】Observer和Observable详解
- N个降序数组,找到最大的K个数TOP K
- 《SpringBoot学习-入门》
- 一次软件测试的电话面试分享
- 关于snprintf函数