//OGNLAction
package
com.test.web.action; 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 ognl.Ognl; import ognl.OgnlParser; import org.struts2.action.utils.BaseAction; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.ValueStack; import com.test.web.bean.User; public class OGNLAction extends BaseAction { private static final long serialVersionUID = -83430767514766103L; private String name = "zhangsan"; public static Integer money = 1000; private User user; private User[] userArray = new User[5]; private List<User> userList = new ArrayList<>(); private Set<User> userSet = new HashSet<>(); private Map<String, User> userMap = new HashMap<>(); public User[] getUserArray() { return userArray; } public void setUserArray(User[] userArray) { this.userArray = userArray; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public Set<User> getUserSet() { return userSet; } public void setUserSet(Set<User> userSet) { this.userSet = userSet; } public Map<String, User> getUserMap() { return userMap; } public void setUserMap(Map<String, User> userMap) { this.userMap = userMap; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getName() { return name; } public String getHello() { return "hello"; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { Map<String, Object> contextMap = ActionContext.getContext() .getContextMap(); ValueStack valueStack = ActionContext.getContext().getValueStack(); System.out.println(valueStack); Map<String, Object> context = valueStack.getContext(); CompoundRoot root = valueStack.getRoot(); System.out.println(root); System.out.println(contextMap.getClass()); System.out.println(context.getClass()); user = new User(); user.setId(1); user.setName("zhangsan"); for (int i = 0; i < 5; i++) { User u = new User(); u.setId(i + 1); u.setName("name" + (i + 1)); userArray[i] = u; userList.add(u); userSet.add(u); userMap.put("name" + (i + 1), u); } putToActionContext("myName", "叶良成"); putToRequest("myName", "赵日天"); putToSession("myName", "陈二狗"); putToApplication("myName", "张全蛋"); putToActionContext("my.name", "王尼玛"); return SUCCESS; } public String hello() { System.out.println("hello world"); return "hello world"; } public static Integer myMoney() { return 10000; } }



//Userbean代码
package com.test.web.bean;

public 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 String sayHello(){
        System.out.println("hello");
        return "hello";
    }
}
 
//jsp页面代码
1
<%@ page pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 <% 4 pageContext.setAttribute("myName", "赵文武"); 5 %> 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 7 <html> 8 <head> 9 <title>struts2</title> 10 </head> 11 <body> 12 <ul> 13 <li style="color:red;font-weight: bold">&nbsp;Liognl 中的算术运算符</li> 14 <li> 15 <label>加法:1 + 1 =</label> 16 <s:property value="1 + 1" /> 17 </li> 18 <li> 19 <label>减法:1 - 1 =</label> 20 <s:property value="1 - 1" /> 21 </li> 22 <li> 23 <label>乘法:5 * 3 =</label> 24 <s:property value="5 * 3" /> 25 </li> 26 <li> 27 <label>除法:4 / 2 =</label> 28 <s:property value="4 / 2" /> 29 </li> 30 <li> 31 <label>求余:4 % 2 =</label> 32 <s:property value="4 % 2" /> 33 </li> 34 <li style="color:red;font-weight: bold">ognl 中的比较运算符</li> 35 <li> 36 <label>大于:4 > 2 =</label> 37 <s:property value="4 > 2" /> 38 </li> 39 <li> 40 <label>小于:4 < 2 =</label> 41 <s:property value="4 > 2" /> 42 </li> 43 <li> 44 <label>等于:4 == 2 =</label> 45 <s:property value="4 == 2" /> 46 </li> 47 <li> 48 <label>大于等于:4 >= 2 =</label> 49 <s:property value="4 >= 2" /> 50 </li> 51 <li> 52 <label>小于等于:4 <= 2 =</label> 53 <s:property value="4 <= 2" /> 54 </li> 55 <li> 56 <label>不等于:4 != 2 =</label> 57 <s:property value="4 != 2" /> 58 </li> 59 <li style="color:red;font-weight: bold">ognl 中的逻辑运算符</li> 60 <li> 61 <label>逻辑与:true && false =</label> 62 <s:property value="true && false" /> 63 <br /> 64 <label>逻辑与:true and false =</label> 65 <s:property value="true and false" /> 66 </li> 67 <li> 68 <label>逻辑或:true || false =</label> 69 <s:property value="true || false" /> 70 <br /> 71 <label>逻辑或:true or false =</label> 72 <s:property value="true or false" /> 73 </li> 74 <li> 75 <label>逻辑非:!true =</label> 76 <s:property value="!true" /> 77 <br /> 78 <label>逻辑非:not true =</label> 79 <s:property value="not true" /> 80 </li> 81 <li style="color:red;font-weight: bold">ognl 中的三元运算符</li> 82 <li> 83 <label>条件运算符:1 > 0 ? 1 : 0 =</label> 84 <s:property value="1 > 0 ? 1 : 0" /> 85 </li> 86 <li style="color:red;font-weight: bold">ognl 调用action中属性(value stack中的属性)</li> 87 <li> 88 <label>action中的name属性=</label> 89 <s:property value="name" /> 90 <br /> 91 <label>action中的user属性=</label> 92 <s:property value="user" /> 93 <br /> 94 <label>action中的user的name属性(使用.操作符)=</label> 95 <s:property value="user.name" /> 96 <br /> 97 <label>action中的user的name属性(使用[]操作符)=</label> 98 <s:property value="user['name']" /> 99 </li> 100 <li style="color:red;font-weight: bold">ognl 调用action中成员方法</li> 101 <li> 102 <label>action中的hello方法=</label> 103 <s:property value="hello()" /> 104 </li> 105 <li style="color:red;font-weight: bold">ognl 调用javabean中成员方法</li> 106 <li> 107 <label>action中user的sayHello()方法=</label> 108 <s:property value="user.sayHello()" /> 109 </li> 110 <li style="color:red;font-weight: bold">ognl 调用action中的静态成员</li> 111 <li> 112 <label>action中静态属性money =</label> 113 <s:property value="@com.test.web.action.OGNLAction@money" /> 114 <br /> 115 <label>action中静态方法myMoney() =</label> 116 <s:property value="@com.test.web.action.OGNLAction@myMoney()" /> 117 </li> 118 <li style="color:red;font-weight: bold">ognl 调用JDK中自带类型的静态成员</li> 119 <li> 120 <label>Math中的PI =</label> 121 <s:property value="@java.lang.Math@PI" /> 122 <br /> 123 <label>Math中的random() =</label> 124 <s:property value="@java.lang.Math@random()" /> 125 </li> 126 <li style="color:red;font-weight: bold">ognl 操作集合数组</li> 127 <li style="color:red;font-weight: bold">数组</li> 128 <li> 129 <label>访问数组userArray</label> 130 <s:property value="userArray" /> 131 <br /> 132 <label>访问数组userArray的长度</label> 133 <s:property value="userArray.length" /> 134 <br /> 135 <label>访问数组userArray的指定的下标的元素</label> 136 <s:property value="userArray[0]" /> 137 <s:property value="userArray[0].name" /> 138 <s:property value="userArray[0]['name']" /> 139 </li> 140 <li style="color:red;font-weight: bold">List集合</li> 141 <li> 142 <label>访问集合userList</label> 143 <s:property value="userList" /> 144 <br /> 145 <label>访问集合userList的长度(一)</label> 146 <s:property value="userList.size" /> 147 <br /> 148 <label>访问集合userList的长度(二)</label> 149 <s:property value="userList.size()" /> 150 <br /> 151 <label>访问集合userList的指定下标元素</label> 152 <s:property value="userList[0].name" /> 153 <br /> 154 <label>访问集合userList的指定下标元素</label> 155 <s:property value="userList.get(0).name" /> 156 <br /> 157 <label>创建一个List集合</label> 158 <s:property value="{1,2,3,4,5}.get(0)" /> 159 <s:property value="{'name1','name2','name3'}.get(0)" /> 160 </li> 161 <li style="color:red;font-weight: bold">set集合</li> 162 <li> 163 <label>访问集合userSet</label> 164 <s:property value="userSet" /> 165 <br /> 166 <label>访问集合userSet的长度</label> 167 <s:property value="userSet.size" /> 168 <br /> 169 <label>访问集合userSet的长度</label> 170 <s:property value="userSet.size()" /> 171 <br /> 172 </li> 173 <li style="color:red;font-weight: bold">map集合</li> 174 <li> 175 <label>访问集合userMap</label> 176 <s:property value="userMap" /> 177 <br /> 178 <label>访问集合userMap的长度</label> 179 <s:property value="userMap.size" /> 180 <br /> 181 <label>访问集合userMap的长度</label> 182 <s:property value="userMap.size()" /> 183 <br /> 184 <label>访问集合userMap的指定key的元素</label> 185 <s:property value="userMap.name1.name" /> 186 <s:property value="userMap['name1']['name']" /> 187 <br /> 188 <label>创建一个map集合</label> 189 <s:property value="#{'name1':'zhangsan','name2':'lisi'}.name1" /> 190 </li> 191 <li style="color:red;font-weight: bold">集合的投影(过滤)</li> 192 <li> 193 <label>获取集合中指定属性子集</label> 194 <s:property value="userList.{name}.get(0)" /> 195 <s:property value="userList.{['name']}[0]" /> 196 <br /> 197 <label>? 获取集合中所有符合条件元素(#this代表当前便利的元素) == userList.{? #this.id > 2}</label> 198 <s:property value="userList.{? #this.id > 2}" /> 199 <br /> 200 <label>userList.{? #this.id > 2}[0] ==</label> 201 <s:property value="userList.{? #this.id > 2}[0]" /> 202 <br /> 203 <label>userList.{? #this.id > 2}.get(0) ==</label> 204 <s:property value="userList.{? #this.id > 2}.get(0)" /> 205 <br /> 206 <label>userList.{? #this.id > 2}.get(0).name ==</label> 207 <s:property value="userList.{? #this.id > 2}.get(0).name" /> 208 <br /> 209 <label>userList.{? #this.id > 2}.{name} ==</label> 210 <s:property value="userList.{? #this.id > 2}.{name}" /> 211 <br /> 212 <br /> 213 <br /> 214 <label>^ 获取集合中符合条件的第一个元素 == userList.{^ #this.id > 2}</label> 215 <s:property value="userList.{^ #this.id > 2}" /> 216 <s:property value="userList.{^ #this.id > 2}[0].name" /> 217 <br /> 218 <label>$ 获取集合中符合条件的第一个元素 == userList.{$ #this.id > 2}</label> 219 <s:property value="userList.{$ #this.id > 2}" /> 220 <s:property value="userList.{$ #this.id > 2}[0].name" /> 221 </li> 222 <li style="color:red;font-weight: bold">ognl 访问Stack context中的属性(需在要访问的属性前加#)</li> 223 <li> 224 <label>访问stack context中的myName</label> 225 <!-- ognl访问属性时,属性前不带#会到值栈中查找,如果没有,会到context中继续查找 --> 226 <s:property value="my.name" /> 227 <s:property value="#myName" /> 228 <br /> 229 <label>访问request中的myName</label> 230 <s:property value="#request.myName" /> 231 <br /> 232 <label>访问session中的myName</label> 233 <s:property value="#session.myName" /> 234 <br /> 235 <label>访问application中的myName</label> 236 <s:property value="#application.myName" /> 237 <br /> 238 <label>访问parameters中的请求参数</label> 239 <s:property value="#parameters.myName" /> 240 <br /> 241 <label>通过attr访问作用域属性 取值顺寻page->request->session->application</label> 242 <s:property value="#attr.myName" /> 243 <br /> 244 </li> 245 <li style="color:red;font-weight: bold">ognl %{expression}操作符,将{}中的字符串作为ongl表达式计算</li> 246 <li> 247 <s:property value="%{name}" /> 248 </li> 249 </ul> 250 <s:debug /> 251 </body> 252 </html>