struts.xml简单配置
<!-- (默认false)设置ognl表达式是否支持静态方法 --><constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant><package name="ognl" namespace="/ognl" extends="struts-default"><action name="ognl" class="com.ouc.wkp.action.OgnlAction"><result>/ognl.jsp</result></action></package>
记得打开
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
package com.ouc.wkp.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 org.apache.commons.chain.Catalog;import com.opensymphony.xwork2.ActionSupport; import com.ouc.wkp.model.Cat; import com.ouc.wkp.model.Dog; import com.ouc.wkp.model.User2;//user.xxx只有传,才会构造,想初始化domain model,可以自己new,也可以传参数值 //但这时候要有参数为空的构造方法 // public class OgnlAction extends ActionSupport {private String password;private String username;private User2 user;private Cat cat;private Map<String, Dog> dogMap = new HashMap<String, Dog>();private Set<Dog> dogs = new HashSet<Dog>();private List<User2> users = new ArrayList<User2>();public Cat getCat() {return cat;}public void setCat(Cat cat) {this.cat = cat;}public User2 getUser() {return user;}public void setUser(User2 user) {this.user = user;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Map<String, Dog> getDogMap() {return dogMap;}public void setDogMap(Map<String, Dog> dogMap) {this.dogMap = dogMap;}public Set<Dog> getDogs() {return dogs;}public void setDogs(Set<Dog> dogs) {this.dogs = dogs;}public List<User2> getUsers() {return users;}public void setUsers(List<User2> users) {this.users = users;}public OgnlAction() {users.add(new User2(1));users.add(new User2(2));users.add(new User2(3));dogs.add(new Dog("dog1"));dogs.add(new Dog("dog2"));dogs.add(new Dog("dog3"));dogMap.put("dog100", new Dog("dog100"));dogMap.put("dog101", new Dog("dog101"));dogMap.put("dog102", new Dog("dog102"));}@Overridepublic String execute() throws Exception {return SUCCESS;}public String m() {return "mmmmm";} }
package com.ouc.wkp.model; //user.xxx只有传,才会构造,想初始化domain model,可以自己new,也可以传参数值 //但这时候要有参数为空的构造方法 public class User2 {private int age=8;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public User2(){ // System.out.println("user constructor"); }public User2(int age){this.age=age;}@Overridepublic String toString() {return "user"+age;} }
package com.ouc.wkp.model;public class Cat {private Dog friend;public Dog getFriend() {return friend;}public void setFriend(Dog friend) {this.friend = friend;}public String miaomiao(){return "miaomiao";}// public Cat(){ // // } // // public Cat(Dog friend){ // this.friend=friend; // } // // @Override // public String toString() { // return "cat"+friend; // } }
package com.ouc.wkp.model;public class Dog {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Dog(){}public Dog(String name){super();this.name=name;}@Overridepublic String toString() {return "dog"+name;} }
然后就是比较核心的ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"><title>My JSP 'ognl.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><s:debug></s:debug>This is ognl.jsp<br> 访问值栈中的action的普通属性:username=<s:property value="username" /><br /> 访问值栈中对象的普通属性(get set 方法)<s:property value="user.age" /><br /> 访问值栈中对象的普通属性(get set 方法)<s:property value="cat.friend.name" /><br /> 访问值栈中对象的普通方法<s:property value="password.length()" /><br /> 访问值栈中对象的普通方法<s:property value="cat.miaomiao()" /><br /> 访问静态方法:<s:property value="@com.ouc.wkp.action.S@s()" /><br /> 访问静态属性:<s:property value="@com.ouc.wkp.action.S@STR" /><br /> 访问Math类的静态方法:<s:property value="@@max(2,3)" /><br /> 访问普通类的构造方法:<s:property value="new com.ouc.wkp.model.User2(10)" /><br /> 访问List:<s:property value="users" /><br /> 访问List中某个元素:<s:property value="users[1]" /><br /> 访问List中元素某个属性的集合:<s:property value="users.{age}" /><br /> 访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]" /><br /> |<s:property value="users[0].age" /><br /> 访问Set:<s:property value="dogs" /><br /> 访问Set中某个元素:<s:property value="dogs[1]" /><br /> 访问Map:<s:property value="dogMap" /><br /> 访问Map中某个元素:<s:property value="dogMap.dog101" /><br /> |<s:property value="dogMap['dog101']" /><br /> |<s:property value="dogMap[\"dog101\"]" /><br /> 访问Map中所有的key:<s:property value="dogMap.keys" /><br /> 访问Map中所有的value:<s:property value="dogMap.values" /><br /> 访问容器的大小:<s:property value="dogMap.size()" /><br /> |<s:property value="users.size" /><br /> 投影(过滤):<s:property value="users.{?#this.age==1}[0]" /><br /> 投影:<s:property value="users.{^#this.age>1}.{age}" /><br /> 投影:<s:property value="users.{$#this.age>1}.{age}" /><br /> 投影:<s:property value="users.{$#this.age>1}.{age}==null" /><br /> []:value stack从上往下数<s:property value="[0].username" /> </body> </html>
输入网址
http://localhost:8081/maShiBing/ognl/ognl?password=p&username=u&user.age=9&cat.friend.name=fuck
运行结果为