OGNL:对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,不但可以任意存取对象的属性还可以调用对象的方法,能够遍历整个对象的结构图,实现对象属性类型的转换等功能。
先看看Jsp页面ognl_info.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OGNL的使用</title>
</head>
<body>
<h3>OGNL的使用</h3><hr/>
访问Action中的普通属性: <s:property value="loginname"/><br/>
访问Action中的对象属性: <s:property value="user.birthday"/><br/>
访问Action中的Set属性: <s:property value="courseSet.toArray()[0]"/><br/>
访问Action中的List属性: <s:property value="list[0]"/><br/>
访问Action中的Map属性的键: <s:property value="map.keys.toArray()[1]"/><br/>
访问Action中的Map属性的值: <s:property value="map.values.toArray()[1]"/><br/>
访问Action中的Map属性的指定键对应的值: <s:property value="map['z']"/><br/>
访问Action中的Map属性的大小: <s:property value="map.size"/><br/>
<hr/>
访问ActionContext中的普通属性:<s:property value="#inte"/><br/>
访问ActionContext中的对象属性:<s:property value="#user2.loginname"/><br/>
<hr/>
访问Action中的普通方法:<s:property value="getAppName()"/><br/>
访问ActionContext中的某个对象上的普通方法:<s:property value="#()"/><br/>
<hr/>
访问静态属性:<s:property value="@java.lang.Math@PI"/><br/>
访问静态方法:<s:property value="@java.lang.Math@floor(44.56)"/><br/>
访问Math类中的静态方法:<s:property value="@@floor(44.56)"/><br/>
<hr/>
调用java.util.Date的构造方法:<s:date name="new java.util.Date()" format="yyyy-MM-dd HH:mm:ss"/><br/>
调用java.util.Date的构造方法创建对象,再调用它的方法:<s:property value="new java.util.Date().getTime()"/><br/>
<hr/>
投影查询:获取userList中所有loginname的列表:<s:property value="userList.{loginname}"/><br/>
选择查询:获取userList中所有score大于60的loginname列表:<s:property value="userList.{?#this.score>60.0}.{loginname}"/><br/>
选择查询:获取userList中所有score大于60并且gender为true的loginname列表:<s:property value="userList.{?(#this.score>60.0 && #this.gender)}.{loginname}"/><br/>
选择查询:获取userList中所有score大于60并且gender为true的第一个元素的loginname:<s:property value="userList.{^(#this.score>60.0 && #this.gender)}.{loginname}"/><br/>
选择查询:获取userList中所有score大于60并且gender为true的最后一个元素的loginname:<s:property value="userList.{$(#this.score>60.0 && #this.gender)}.{loginname}"/><br/>
<hr/>
访问名为xxx的请求参数对应的第一个值:<s:property value="#[0]"/><br/>
访问通过ActionContext中放入Request中的属性:<s:property value="#request.reqAtt"/><br/>
访问通过ServletActionContext中放入Request中的属性:<s:property value="#request.reqAtt2"/><br/>
访问通过ActionContext中放入Session中的属性:<s:property value="#session.sesAtt"/><br/>
访问通过ServletActionContext中放入Session中的属性:<s:property value="#session.sesAtt2"/><br/>
访问通过ActionContext中放入ServletContext中的属性:<s:property value="#application.appAtt"/><br/>
访问通过ServletActionContext中放入ServletContext中的属性:<s:property value="#application.appAtt2"/><br/>
直接访问属性域中指定名称的属性对应的值:<s:property value="#attr.sesAtt2"/><br/>
<br/><br/><hr/>
<s:iterator value="userList" status="vs">
<s:if test="%{#vs.odd}">
<span >
<s:property value="#vs.index"/>: <s:property value="loginname"/>,<s:date name="birthday" format="yyyy-MM-dd HH:mm:ss"/><br/>
</span>
</s:if>
<s:else>
<span >
<s:property value="#vs.index"/>: <s:property value="loginname"/>,<s:date name="birthday" format="yyyy-MM-dd HH:mm:ss"/><br/>
</span>
</s:else>
</s:iterator>
<hr/><s:debug/>
</body>
</html>
User实体类User.java:
package com.tjcyjd.web.action;
import java.util.Date;
/**
* 实体类
*
*
*/
public class User {
private Integer id;
private String loginname;
private Double score;
private Boolean gender;
private Character cha;
private Date birthday;
public User() {
}
public User(Integer id, String loginname, Double score, Boolean gender,
Character cha, Date birthday) {
this.id = id;
this.loginname = loginname;
this.score = score;
this.gender = gender;
this.cha = cha;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Character getCha() {
return cha;
}
public void setCha(Character cha) {
this.cha = cha;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String info() {
return "User [birthday=" + birthday + ", cha=" + cha + ", gender="
+ gender + ", id=" + id + ", loginname=" + loginname
+ ", score=" + score + "]";
}
}
Action类OGNLAction:
package com.tjcyjd.web.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* OGNL Action类
*
*/
public class OGNLAction extends ActionSupport {
private static final long serialVersionUID = -2554018432709689579L;
private String loginname;
private String pwd;
private User user;
private Set<String> courseSet;
private List<String> list;
private Map<String, String> map;
private List<User> userList;
public String execute() throws Exception {
this.loginname = "李晓红";
this.user = new User(123, "柳梦璃", 88.9, true, 'B', new Date());
this.courseSet = new LinkedHashSet<String>();
this.courseSet.add("corejava");
this.courseSet.add("JSP/Servlet");
this.courseSet.add("S2SH");
this.list = new ArrayList<String>(this.courseSet);
this.map = new HashMap<String, String>();
this.map.put("x", "xxx");
this.map.put("y", "yyy");
this.map.put("z", "zzz");
ActionContext context = ActionContext.getContext();
context.put("uname", "云天河");
context.put("inte", Integer.valueOf(888888));
context
.put("user2",
new User(123, "韩菱纱", 99.1, false, 'B', new Date()));
this.userList = new ArrayList<User>();
this.userList.add(new User(1, "zs", 48.9, true, 'D', new Date()));
this.userList.add(new User(2, "ls", 68.1, true, 'C', new Date()));
this.userList.add(new User(3, "ww", 78.2, false, 'B', new Date()));
this.userList.add(new User(4, "zl", 88.3, true, 'A', new Date()));
// -----------------------------------------------------------------
// 推荐方式:不会跟Servlet API耦合
context.put("reqAtt", "往ActionContext中put的属性");
context.getSession()
.put("sesAtt", "往ActionContext.getSession()中put的属性");
context.getApplication().put("appAtt",
"往ActionContext.getApplication()中put的属性");
ServletActionContext.getRequest().setAttribute("reqAtt2",
"Request作用域中的属性");
ServletActionContext.getRequest().getSession().setAttribute("sesAtt2",
"Session作用域中的属性");
ServletActionContext.getServletContext().setAttribute("appAtt2",
"Application作用域中的属性");
return SUCCESS;
}
public String getAppName() {
return "这是OGNL的使用示例代码";
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set<String> getCourseSet() {
return courseSet;
}
public void setCourseSet(Set<String> courseSet) {
this.courseSet = courseSet;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}