OGNL:
查询struts2中的配置信息:
Struts2-core-2.1.6.jar-àorg.apache.struts2--àdefault.properties
对应的Action,OGNLActiion.java,对于其中的一些属性类,如userList,bookSet,orgMap都为作声明:
package com.oristand.actions;
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 ONGLAction extends ActionSupport {
//private Cat cat;
private User user;
private String username;
private String password;
private List<User> userList = new ArrayList<User>();
private Set<Book> bookSet = new HashSet<Book>();
private Map<String,Org> orgMap = new HashMap<String,Org>();
public ONGLAction() {
userList.add(new User(20));
userList.add(new User(21));
userList.add(new User(22));
bookSet.add(new Book("史记"));
bookSet.add(new Book("春秋"));
bookSet.add(new Book("战国"));
orgMap.put("第一名", new Org("东方标准"));
orgMap.put("第二名", new Org("尚学堂"));
orgMap.put("第三名", new Org("传智播客"));
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
// public Cat getCat() {
// return cat;
// }
// public void setCat(Cat cat) {
// this.cat = cat;
// }
public Set<Book> getBookSet() {
return bookSet;
}
public Map<String, Org> getOrgMap() {
return orgMap;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public List<User> getUserList() {
return userList;
}
public String getUsername() {
return username;
}
public void setBookSet(Set<Book> bookSet) {
this.bookSet = bookSet;
}
public void setOrgMap(Map<String, Org> orgMap) {
this.orgMap = orgMap;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void setUsername(String username) {
this.username = username;
}
}
相应的ognl.jsp:
from value stack: username is:<s:property value="username"/><br>
from value stack: password is:<s:property value="password"/><br>
from value stack: user.age is:<s:property value="user.age"/><br>
from value stack: 【user.cat.friend.name】 is:<s:property value="user.cat.friend.name"/><br>
from value stack: user.cat.friend.name's length is:<s:property value="user.cat.friend.name.length()"/><br>
from value stack: 【user.cat.miaomiao()】 is:<s:property value="user.cat.miao()"/><br>
<hr/>
测试static属性,【@com.oristand.actions.Sta@str】 is :<s:property value="@com.oristand.actions.Sta@str"/><br/>
测试static方法,【@com.oristand.actions.Sta@s()】is :<s:property value="@com.oristand.actions.Sta@s()"/><br><br>
<hr/>
new com.oristand.actions.User(8)" is: <s:property value="new com.oristand.actions.User(8)"/><br><br>
<hr/>
访问userList: <s:property value="userList"/><br>
访问userList中的第二个元素:<s:property value="userList[1]"/><br>
将usreList中的age属性再封装成一个list,【userList.{age} is】:<s:property value="userList.{age}"/><br