一. OGNL表达对各种方法的调用
1. 调用静态方法
我们创建一个Utils工具类,里面有2个静态方法:
public static String toUpperCase(String str) {
return str.toUpperCase();
}
public static String toLowerCase(String str) {
return str.toLowerCase();
}
在action类中添加一个方法staticCalls: public String staticCalls() {
username = "qq";
password = "pp";
user = new User();
user.setSex('男');
user.setAge(18);
User u = new User();
u.setUsername("Jerry");
ActionContext.getContext().put("user", u);
return "static";
}
在jsp页面中的使用: 静态方法调用:<br />
1. <s:property value="@com.thr.struts2.tool.Utils@toUpperCase('Hello world!')"/><br />
2. <s:property value="@com.thr.struts2.tool.Utils@toLowerCase('Hello world!')"/><br />
3. <s:property value="@com.thr.struts2.tool.Utils@toUpperCase(username)"/><br />
4. <s:property value="@com.thr.struts2.tool.Utils@toUpperCase(password)"/><br />
5. <s:property value="@com.thr.struts2.tool.Utils@toLowerCase(user.sex)"/><br />
6. <s:property value="@com.thr.struts2.tool.Utils@toLowerCase(user.age)"/><br />
7. <s:property value="@com.thr.struts2.tool.Utils@toLowerCase(#user.username)"/><br />
value中@后要跟一个包名类名,然后@再跟静态方法的名字,括号中是参数。2. 动态方法调用
在Utils中再写一个方法:
public String substring(String str, int start, int end) {
return str.substring(start, end);
}
jsp中调用: 动态方法调用:<br />
1. <s:property value="new com.thr.struts2.tool.Utils().substring('helloworld',2,5)"/><br />
2. 嵌套方法:<br />
<s:property value="@com.thr.struts2.tool.Utils@toLowerCase(new com.thr.struts2.tool.Utils().substring('helloworld',2,5))"/><br />
3. 调用Action中的方法:<br />
<s:property value="getUtils().substring('helloworld', 2, 5)"/>
<s:property value="utils.substring('helloworld', 2, 5)"/><br />
4. <s:property value="out(#user.username)"/><br />
5. <s:property value="getUsername()"/><br />
使用静态方法,需要new 包名类名+方法。第4、5条还可以调用Action中定义的方法:
public Utils getUtils() {
return new Utils();
}
public String out(String msg) {
return "hello" + msg;
}
在调用getXxx方法的时候,实际上struts会默认有一个Xxx成员,即可以直接调用xxx属性方法。在调用属性xxx方法的时候,实际上是调用了它的getXxxx方法。二. OGNL简介
1. 值栈基础
OGNL,Object GraphicNavigation Language
(1). OGNL属于另一个开源框架经过struts2扩展集成进struts2框架
(2). OGNL可以用于JSP标签、界面参数传递到Action中,还可以用于struts2配置文件中
(3). OGNL对象图——可以以任意一个对象为根,通过OGNL可以访问与这个对象关联的其他对象。
(4). Root就是对象图中的跟对象
(5). 对象图导航必须以getter方法进行导航
2. 根对象root的概念
创建这样子的一个测试方法:
public void test3() {
User user = new User();
user.setUsername("张三");
user.setPassword("222");
user.setSex('男');
user.setAge(27);
Address address = new Address();
address.setHomeAddress("西安");
address.setCompanyAddress("北京");
Contact contact = new Contact();
contact.setQq("4022223");
contact.setTel("0291123423");
contact.setPhone("13588434513");
address.setContact(contact);
user.setAddress(address);
try {
// 取值
String qq = (String) Ognl
.getValue("address.contact.qq",user);
String phone = (String)Ognl.getValue(
"#root.contact.phone",address);
System.out.println(qq);
System.out.println(phone);
// 赋值
Ognl.setValue("contact.phone",address, "1371641354");
System.out.println((String)Ognl.getValue("address.contact.phone",
user));
} catch (OgnlException e) {
e.printStackTrace();
}
}
使用getValue方法时,第二个参数表示从层级结构的哪一个开始出发,以此为跟对象,然后一层一层写到第一个参数中。例如上面的从user开始,user底下的address,address底下的contact,contact底下的qq,就取到值了,赋值的方法类似。
也可以使用#root代表当前根对象:例如getValue("#root.contact.phone",address);
3. Context上下文对象
使用Context对象可以访问不能导航的对象:
public void test4() {
User user = new User();
user.setUsername("张三");
user.setPassword("222");
user.setSex('男');
user.setAge(27);
Address address = new Address();
address.setHomeAddress("西安");
address.setCompanyAddress("北京");
Contact contact = new Contact();
contact.setQq("4022223");
contact.setTel("0291123423");
contact.setPhone("13588434513");
address.setContact(contact);
user.setAddress(address);
User user2 = new User();
user2.setUsername("李四");
user2.setPassword("111");
user2.setSex('女');
user2.setAge(22);
User user3 = new User();
user3.setUsername("王五");
user3.setPassword("112");
user3.setSex('男');
user3.setAge(23);
Map<String, Object> context = new HashMap<String, Object>();
context.put("user", user);
context.put("user2", user2);
context.put("user3", user3);
try {
String username = (String) Ognl.getValue("#user.username", context,
new Object());
System.out.println(username);
Ognl.setValue("#user.username", context, new Object(), "哈哈");
System.out.println(Ognl.getValue("#user.username", context,
new Object()));
System.out.println(Ognl.getValue("#user.username + ', ' + #user2.password + \", \" +#user3.sex ", context,
new Object()));
} catch (OgnlException e) {
e.printStackTrace();
}
}
可以看出OGNL的表达式还是相当的强大的,几乎可以和使用java代码一样随意。我们只需要创建一个map集合,将互相不关联的对象放入这个集合中即可访问。4. 测试调用方法
使用OGNL测试调用方法:
public void test5() {
User user = new User();
user.setUsername("张三");
user.setPassword("222");
user.setSex('男');
user.setAge(27);
Address address = new Address();
address.setHomeAddress("西安");
address.setCompanyAddress("北京");
Contact contact = new Contact();
contact.setQq("4022223");
contact.setTel("0291123423");
contact.setPhone("13588434513");
address.setContact(contact);
user.setAddress(address);
User user2 = new User();
user2.setUsername("李四");
user2.setPassword("111");
user2.setSex('女');
user2.setAge(22);
User user3 = new User();
user3.setUsername("王五");
user3.setPassword("112");
user3.setSex('男');
user3.setAge(23);
Map<String, Object> context = new HashMap<String, Object>();
context.put("user", user);
context.put("user2", user2);
context.put("user3", user3);
try {
// 调用静态方法
Ognl.getValue("@java.lang.System@out.println(#root.contact.phone)",
address);
// 调用动态方法
String s = (String) Ognl
.getValue(
"new com.thr.struts2.tool.Utils().substring(#root.contact.phone, 1, 5)",
address);
System.out.println(s);
// Context中对象
String p = (String) Ognl.getValue("#user2.getUsername()", context,
new Object());
System.out.println(p);
String n = (String)Ognl.getValue("username = '周六'", user);
System.out.println(n);
} catch (OgnlException e) {
e.printStackTrace();
}
}
5. 对集合的操作
public void test() {
User user = new User();
user.setUsername("张三");
try {
// 创建List
List list = (List) Ognl.getValue("{'Hello', 123, username}", user);
System.out.println(list);
// 创建数组
Object[] objs = (Object[]) Ognl.getValue(
"new Object[]{'Hello', 123, username}", user);
System.out.println(objs);
// 创建map集合
Map<String, Object> map = (Map<String, Object>) Ognl.getValue(
"#{'AA':123,'BBB':234, username:111, 'list':{'Hello', 123, username} }", user);
System.out.println(map);
// 访问map
Map<String, Object> context = new HashMap<String, Object>();
context.put("list", list);
context.put("objs", objs);
context.put("map", map);
System.out.println(context);
// 访问list
Ognl.getValue("@java.lang.System@out.println(#list[2])", context, new Object());
// 访问数组
Ognl.getValue("@java.lang.System@out.println(#objs[0])", context, new Object());
// 访问Map
Ognl.getValue("@java.lang.System@out.println(#map['AA'])", context, new Object());
Ognl.getValue("@java.lang.System@out.println(#map.BBB)", context, new Object());
Ognl.getValue("@java.lang.System@out.println(#map.list)", context, new Object());
Ognl.getValue("@java.lang.System@out.println(#map.list[2])", context, new Object());
System.out.println("---");
// 访问根节点为list
Ognl.getValue("@java.lang.System@out.println(size())", context, list);
Ognl.getValue("@java.lang.System@out.println(get(0))", context, list);
Ognl.getValue("@java.lang.System@out.println([1])", context, list);
} catch (OgnlException e) {
e.printStackTrace();
}
}
注意的是:在访问map集合时,如果想使用map.XXX来调用属性时,键的长度必须要大于等于3才可以正常使用。