OGNL4投影&取出值栈中多个action
1. 投影
其实名字叫做投影,并不是特别的难,实际上就是一个过滤的功能,就是说,把符合某些判断条件的选择出来。以《1500》的代码为基础,在ognl.jsp中添加投影语句
<li>过滤users集合中user.age==1的集合中第一个:<s:property value="users.{?#this.age==1}[0]"/></li>
<li>过滤users集合中user.age>1的集合中第一个:<s:property value="users.{^#this.age>1}"/></li>
<li>过滤users集合中user.age>1的集合中最后一个:<s:property value="users.{$#this.age>1}"/></li>
<li>判断users集合中user.age>1的集合中最后一个是否为空:<s:property value="users.{$#this.age>1}==null"/></li>
第一句
<li>过滤users集合中user.age==1的集合中第一个:<s:property value="users.{?#this.age==1}[0]"/></li>
过程如下:
取出users集合中每一个成员,判断这个成员.age==1?true则放入集合,false则continue。然后再取出得到的集合中的第一个成员。
第二句相似,不过^#
的意思是取满足条件的第一个。
此例解释如下:
<li>过滤users集合中user.age>1的集合中第一个:<s:property value="users.{^#this.age>1}"/></li>
意为拿出users这个List中user的age>1的集合中的第一个成员构成的集合。(这句话听拗口的,具体分析是,list中有三个user,age分别是1,2,3,age>1的user有两个,这两个先组成了一个集合,从这个集合中取出第一个成员,把成员再单独作为一个集合,返出来。)
第三句$#指的是,满足条件的最后一个。(就不再具体解释了)
此部分的运行结果是:
取出值栈中多个action
其实取这么一个名字可能不是非常的确切,但是我确实不知道如何描述这么一个应用。直接从代码进行分析。
在ognl.jsp中添加一句代码
<li>拿到值栈中多个action对象: <s:property value="[0]"/></li>
它代表从值栈中拿到所有对象,其实这个对象不只是action对象,如下图所示
可以看出,这个值栈中有两个action还有一个textprovider,但是为什么会有两个action呢,原因是我之前又做了一些操作。
struts-ognl.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.bjsxt.struts2.ognl.OgnlAction">
<result>/ognl.jsp</result>
</action>
<action name="test" class="com.bjsxt.struts2.ognl.TestAction">
<result type="chain">ognl</result>
</action>
</package>
</struts>
加入了一个新的action,action的result是chain类型,服务器跳转到ognl的action,对应的Action是TestAction
package com.bjsxt.struts2.ognl;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
return super.execute();
}
}
除了返回success之外,没有其他的功能了。
这种情况导致的值栈中有两个action,平常状况下,我们打开值栈,肯定只有一个action。
附录
以下是上文中缺失的完整代码文件,若从前面《1300》开始读起,可选择跳过此部分。
ognl.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>OGNL表达式语言学习</title>
</head>
<body>
<ol>
<li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li>
<li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/>
| <s:property value="user['age']"/>
| <s:property value="user[\"age\"]"/>
| wrong: <%--<s:property value="user[age]"/>--%></li>
<li>访问值栈中的对象的普通属性: <s:property value="cat.friend.name"/></li>
<li>访问值栈中的对象的普通方法: <s:property value="password.length()"/></li>
<li>访问值栈中的对象的普通方法: <s:property value="cat.eat()"/></li>
<li>访问值栈中的对象的普通方法: <s:property value="r_str()"/></li>
<hr/>
<li>访问静态方法: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@r_static_str()"/></li>
<li>访问静态属性: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@static_value"/></li>
<li>访问Math类型静态属性: <s:property value="@@max(2,3)"/></li>
<hr/>
<li>访问List: <s:property value="users"/></li>
<li>访问List中某个元素: <s:property value="users[0]"/></li>
<li>访问List中属性的集合: <s:property value="users.{age}"/></li>
<li>访问List中属性的集合中特定值: <s:property value="users.{age}[0]"/>
|<s:property value="users[0].age"/></li>
<li>访问Set: <s:property value="dogs"/></li>
<li>访问Set中特定元素: <s:property value="dogs[0]"/></li>
<li>访问Map: <s:property value="map_dogs"/></li>
<li>访问Map中某个元素: <s:property value="map_dogs.map1"/></li>
<li>访问Map中所有的key: <s:property value="map_dogs.keys"/></li>
<li>访问Map中所有的value: <s:property value="map_dogs.values"/></li>
<li>访问容器大小: <s:property value="users.size()"/></li>
<hr/>
<li>过滤users集合中user.age==1的集合中第一个:<s:property value="users.{?#this.age==1}[0]"/></li>
<li>过滤users集合中user.age>1的集合中第一个:<s:property value="users.{^#this.age>1}"/></li>
<li>过滤users集合中user.age>1的集合中最后一个:<s:property value="users.{$#this.age>1}"/></li>
<li>判断users集合中user.age>1的集合中最后一个是否为空:<s:property value="users.{$#this.age>1}==null"/></li>
<li>拿到值栈中多个action对象: <s:property value="[0]"/></li>
</ol>
<s:debug></s:debug>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<include file="/com/bjsxt/struts2/ognl/struts-ognl.xml"/>
</struts>
OgnlAction
package com.bjsxt.struts2.ognl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport {
private Cat cat;
private String password;
private User user;
private List<User> users =new ArrayList<User>();
private Set<Dog> dogs=new HashSet<Dog>();
private Map<String,Dog> map_dogs=new HashMap<String,Dog>();
private String username;
public OgnlAction() {
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
dogs.add(new Dog("set1"));
dogs.add(new Dog("set2"));
dogs.add(new Dog("set3"));
map_dogs.put("map1", new Dog("map1"));
map_dogs.put("map2", new Dog("map2"));
map_dogs.put("map3", new Dog("map3"));
}
public OgnlAction(Cat cat, String password, User user, List<User> users,
Set<Dog> dogs, Map<String, Dog> map_dogs, String username) {
super();
this.cat = cat;
this.password = password;
this.user = user;
this.users = users;
this.dogs = dogs;
this.map_dogs = map_dogs;
this.username = username;
}
public String r_str(){
return "action_str";
}
public String execute() {
return SUCCESS;
}
public Cat getCat() {
return cat;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public String getUsername() {
return username;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUsername(String username) {
this.username = username;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public Set<Dog> getDogs() {
return dogs;
}
public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}
public Map<String, Dog> getMap_dogs() {
return map_dogs;
}
public void setMap_dogs(Map<String, Dog> map_dogs) {
this.map_dogs = map_dogs;
}
}
Cat
package com.bjsxt.struts2.ognl;
public class Cat {
private Dog friend;
private String name;
public String eat(){
return "cat eating...";
}
public Dog getFriend() {
return friend;
}
public void setFriend(Dog friend) {
this.friend = friend;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog
package com.bjsxt.struts2.ognl;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog(String name) {
super();
this.name = name;
}
public Dog() {
super();
// TODO Auto-generated constructor stub
}
}
Static_Example
package com.bjsxt.struts2.ognl;
public class Static_Example {
public static String static_value="STATIC_VALUE";
public static String r_static_str(){
return "static method()";
}
}
若有不足之处,请不吝赐教