Struts2语法--Ognl
OGNL: Object Graph Navigation Language
index.jsp:
<body> 访问属性 <a href="<%=contextPath %>/ognl.action?username=u&password=p">ognl</a> </body>
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/ognl.xml"/> </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>
ognl.action:
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 static final long serialVersionUID = 1L;
private Cat cat;
private String password;
private User user;
private String username;
private List<User> users = new ArrayList<User>();
private Set<Dog> dogs = new HashSet<Dog>();
private Map<String, Dog> dogMap = new HashMap<String, Dog>();
public OgnlAction() {
users.add(new User(1));
users.add(new User(2));
users.add(new User(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"));
}
public String execute() {
return SUCCESS;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public Cat getCat() {
return cat;
}
public Set<Dog> getDogs() {
return dogs;
}
public List<User> getUsers() {
return users;
}
public Map<String, Dog> getDogMap() {
return dogMap;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}
public String m() {
return "hello";
}
}
ognl.jsp访问:
<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>访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/></li>
<li>访问值栈中对象的普通方法:<s:property value="password.length()"/></li>
<li>访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" /></li>
<li>访问值栈中action的普通方法:<s:property value="m()" /></li>
<hr />
<li>访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/></li>
<li>访问静态属性:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/></li>
<li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li>
<hr />
<li>访问普通类的构造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)"/></li>
<hr />
<li>访问List:<s:property value="users"/></li>
<li>访问List中某个元素:<s:property value="users[1]"/></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[1]"/></li> <!-- 取不到,因为set无序 -->
<li>访问Map:<s:property value="dogMap"/></li>
<li>访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li>
<li>访问Map中所有的key:<s:property value="dogMap.keys"/></li>
<li>访问Map中所有的value:<s:property value="dogMap.values"/></li>
<li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>
<hr />
<li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>
<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>
<hr />
<li>[]:<s:property value="[0].username"/></li> <!-- 访问值栈里第一个元素:action -->
</ol>
<s:debug></s:debug>
</body>
对于user.age需要传值进去才给构造函数赋值,或者在action里给user进行实例化, domain model里要有一个参数为空的构造方法:
private User user=new User();
静态方法和静态属性的类:
package com.bjsxt.struts2.ognl; public class S { public static String STR = "STATIC STRING"; public static String s() { return "static method"; } }
URL访问的时候:
http://localhost:8080/Struts2_1900_OGNL/ognl.action?username=u&password=p&user.age=9&cat.friend.name=oudy
结果:
1.访问值栈中的action的普通属性: username = u 2.访问值栈中对象的普通属性(get set方法): | | | wrong: 3.访问值栈中对象的普通属性(get set方法): 4.访问值栈中对象的普通方法:1 5.访问值栈中对象的普通方法: 6.访问值栈中action的普通方法:hello -------------------------------------------------------------------------------- 7.访问静态方法:static method 8.访问静态属性:STATIC STRING 9.访问Math类的静态方法:3 -------------------------------------------------------------------------------- 10.访问普通类的构造方法:user8 -------------------------------------------------------------------------------- 11.访问List:[user1, user2, user3] 12.访问List中某个元素:user2 13.访问List中元素某个属性的集合:[1, 2, 3] 14.访问List中元素某个属性的集合中的特定值:1 | 1 15.访问Set:[dog: dog1, dog: dog2, dog: dog3] 16.访问Set中某个元素: 17.访问Map:{dog102=dog: dog102, dog101=dog: dog101, dog100=dog: dog100} 18.访问Map中某个元素:dog: dog101 | dog: dog101 | dog: dog101 19.访问Map中所有的key:[dog102, dog101, dog100] 20.访问Map中所有的value:[dog: dog102, dog: dog101, dog: dog100] 21.访问容器的大小:3 | 3 -------------------------------------------------------------------------------- 22.投影(过滤):user1 23.投影:[2] 24.投影:[3] 25.投影:false -------------------------------------------------------------------------------- 26.[]:u