在struts2中大量使用了OGNL 语法,当然,想学好struts当然要了解OGNL,于是开始学习了OGNL之旅。废话不多说。
直接切入主题。
我将根据OGNL的作用一一阐述和举例:
首先:
一些基本语法:
- 常量: 字符串:“ hello ” 字符:‘ h ’ 数字:除了像 java 的内置类型 int,long,float 和 double,Ognl 还有如例:10.01B,相当于 java.math.BigDecimal,使用’ b ’或者’ B ’后缀。 100000H,相当于 java.math.BigInteger,使用’ h ’ 或 ’ H ’ 后缀。
- 属性的引用 例如:user.name
- 变量的引用 例如:#name
- 静态变量的访问 使用 @class@field
- 静态方法的调用 使用 @class@method(args), 如果没有指定 class 那么默认就使用 java.lang.Math.
- 构造函数的调用 例如:new java.util.ArrayList();
一,读取类属性
二,写类属性
三,读写常量
四,用构造函数创建集合类
/**
* <pre>
* 测试构造collection
* 参考http://www.opensymphony.com/ognl/html/LanguageGuide/projection.html
* </pre>
*
* @author yajun.wuyj
*/
public class OgnlCollectionConstructTest {
public static void main(String[] args) throws OgnlException {
// 创建链表
List list = (List) Ognl.getValue("{ null,\"Untitled\"}", null);
System.out.println(list);
// 创建数组
int[] array = (int[]) Ognl.getValue("new int[] { 1, 2, 3 }", null);
System.out.println(array[2]);
// 创建map
Map map = (Map) Ognl.getValue("#{ \"foo\" :\"foo value\", \"bar\" : \"bar value\" }", null);
System.out.println(map);
}
}
五,
Projecting Across Collections
/**
* <pre>
* 测试针对所有的elements 调用方法
* 参考http://www.opensymphony.com/ognl/html/LanguageGuide/projection.html
* </pre>
*
* @author Administrator
*/
public class OgnlProjectingTest {
public static void main(String[] args) throws OgnlException {
List<String> strings = new ArrayList<String>();
strings.add("1");
strings.add("2");
strings.add("3");
System.out.println(Ognl.getValue("{ #this instanceof String ? #this : #this.toString()}", strings));
}
}
六,
Selecting From Collections
/**
* <pre>
* 挑选指定的元素作为新的的集合
* 参考:http://www.opensymphony.com/ognl/html/LanguageGuide/selection.html
* </pre>
*
* @author Administrator
*/
public class OgnlSelectingTest {
public static void main(String[] args) throws OgnlException {
OgnlContext context = new OgnlContext();
List lists = new ArrayList();
lists.add("string 1");
lists.add(Integer.valueOf(1));
lists.add("string 2");
context.put("lists", lists);
// 找出所有的string
System.out.println(Ognl.getValue("lists.{? #this instanceof String}", context));
// 找出第一个string
System.out.println(Ognl.getValue("lists.{^ #this instanceof String}", context));
// 找出最后一个string
System.out.println(Ognl.getValue("lists.{$ #this instanceof String}", context));
}
}
==========================
struts2中的context中,如果多个属性相同???
分享到:
相关推荐
关于OGNL表达式的使用的整理以及提供的文档说明,ps:文档说明是英文的,可能需要翻译
struts标签和ognl语法,比较全的标签说明
根据OpenSymphony的OGNL官方文档,整理了下。简洁易懂
ognl表达式 ognl表达式 ognl表达式 ognl表达式
struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用
主要介绍了OGNL表达式基本语法与用法详解,具有一定参考价值。需要的朋友可以了解下。
ognl2.6.11源代码以及api英文文档
本案例提供ognl使用详解,测试通过,只需解压放入自己WEB项目中,执行struts_ognl包内java文件即可(未提供jia包,若需要可以联系留言发至邮箱),若测试不好可以联系本人提供指导. Struts 2默认的表达式语言是OGNL...
ognl语言的使用。让我们通过使用ognl了解ognl标签使用的便利性。
struts符号的$,%,#使用,教你怎样使用struts中OGNL表达式。
OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科
开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12...
文档详细描述了Velocity整合struts2步骤,以及velocity的语法规范
camel-ognl-1.6.4.jar, camel-ognl-2.8.1.jar, com.springsource.org.ognl-2.6.9.jar, com.springsource.org.ognl-sources-2.6.9.jar, ognl-2.5.1.jar, ognl-2.6.11.jar, ognl-2.6.3.jar, ognl-2.6.5.jar, ognl-...
使用的ognl,正对是struts2 hibernate 等
OGNL 最初是为了能够使用对象的属性名来建立 UI 组件 (component) 和 控制器 (controllers) 之间的联系,简单来说就是:视图 与 控制器 之间数据的联系。后来为了应付更加复杂的数据关系,Drew Davidson 发明了一个...
java的Struts中的OGNL标签的使用,详细介绍,比较实用。
ognl源码 ognl源码 ognl源码 ognl源码 ognl源码
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,...
3. 语法 4. 表达式 常数 属性引用 索引 数组(Array)与列表(List)的索引 JavaBean中的索引属性(Indexed Properties) OGNL特有的对象索引属性(Object Indexed Properties) 调用方法 变量引用 带括号的表达式...