<div>
<s:set var="scoresGroup1" value="{97, 98, 56, 67}" />
<s:set var="scoresGroup2" value="{96, 88, 91}" />
<s:append var="scoresAll">
<s:param value="#scoresGroup1" />
<s:param value="#scoresGroup2" />
s:append>
<s:iterator value="#scoresAll">
<s:property />
s:iterator>
<br >
<s:merge var="scoresAll2">
<s:param value="#scoresGroup1" />
<s:param value="#scoresGroup2" />
s:merge>
<s:iterator value="#scoresAll2">
<s:property />
s:iterator>
div>
<%
StudentConverter converter = new StudentConverter();
request.setAttribute("myStudentConverter", converter);
%>
<s:generator separator="," converter="#request.myStudentConverter" val="'S1013#张三#24#男,S1014#李四#23#女,S1015#王五#25#男'" var="stus" />
<s:iterator value="#stus" var="stu">
学号:${ stu.sno },姓名:${ stu.name }<br>
s:iterator>
<s:set var="count" value="1" />
<table border="1">
<tr>
<td width="150">图书td>
<td width="100">价格td>
<td width="100">分类td>
tr>
<s:iterator value="#books.{? #this.price lt 100}" var="b" status="sta">
<tr style="background-color: ${sta.odd ? 'pink' : 'yellow'}">
<td width="150">${pageScope.count }:《${ b.title }》td>
<td width="100">¥${b.price }td>
<td width="100">${ b.category.name }td>
tr>
<s:set var="count" value="#count + 1" />
s:iterator>
table>
public void testOnglBasic() throws OgnlException {
Map<String, Object> context = new HashMap<String, Object>();
List<Book> list = new ArrayList<Book>();
list.add(new Book(1001, "西游记", "无尘恩", 56.0, "人民教育出版社", "xyj.png", new Category(1, "小说")));
list.add(new Book(1002, "红楼梦", "曹雪芹", 98.0, "清华大学出版社", "xyj.png", new Category(1, "小说")));
list.add(new Book(1090, "水浒传", "施耐庵", 105.0, "人民教育出版社", "xyj.png", new Category(1, "小说")));
list.add(new Book(1078, "三国演义", "罗贯中", 28.5, "清华大学出版社", "xyj.png", new Category(1, "小说")));
Book book = new Book(1001, "西游记", "无尘恩", 56.0, "人民教育出版社", "xyj.png", new Category(1, "小说"));
Map<String, Object> datas = new HashMap<String, Object>();
datas.put("bk-101", book);
datas.put("myBooks", list);
context.put("teacher", "Mr Jia");
context.put("ds", datas);
// String expression = "title"; // 默认取值为root根对象的属性
// String expression = "#b"; // 已#开头的表达式表示从context(Map)中找键为#开头的数据(b)
// OGNL支持数据类型:字符类、数字、boolean、null
// String expression = "3 + 5 > 5 * 6 or 5 > title.length()"; //支持算术、关系、逻辑等运算符
// String expression = "title.length() > 2 ? 'ok' : 'no'"; // 支持三元运算符
// String expression = "#b.title"; // 允许 "值.属性"
// String expression = "#b.title.substring(1)"; // 允许 "值.方法(...)"
// String expression = "@java.lang.Math@PI"; // 支持静态字段调用
// String expression = "@java.lang.Math@random()"; //支持静态方法调用
// String expression = "new java.lang.String('abc').class.name"; // 支持创建对象
// String expression = "#myBooks[0].title.length()"; // 支持用[index]取列表中的元素
// String expression = "#ds['bk-101']"; // 支持用map['keyName']取Map中的值(keyName必须长度大于1),也支持map.keyName取Map中的值(keyName必须是标识符)
// String expression = "{'西游记', '水浒中', '红楼梦', '跟贾老师学Java'}.size()"; // 支持用{ elem1, ...., elemN} 创建一个ArrayList
// String expression = "#{ '语文': 98, '数学': 87, '英语': 92 }"; // 支持用#{ 'key1': value1, 'key2': value2, .... 'keyN': valueN}
// String expression = "#ds.myBooks.{$ #this.price lt 100 and #this.publisher eq '清华大学出版社'}"; // 支持过滤: ? ^ $
String expression = "#ds.myBooks.{ #this.price }"; // 支持投影
Object value = Ognl.getValue(expression, context, new Object());
System.out.println(value);
}
注意在取值的时候
value=”#…” 和 value=”…”,带“#”是从context中获取值,没有的是从根对象XxxAction中获取