ognlShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'ognlShow.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>
<hr>
使用OGNL访问值栈的context对象<br>
#request.msgWelcome1:<s:property value="#request.msgWelcome1"/><br>
#request.msgWelcome:<s:property value="#request.msgWelcome"/><br>
#session.msgWelcome:<s:property value="#session.msgWelcome"/><br>
#application.msgWelcome:<s:property value="#application.msgWelcome"/><br>
#attr.msgWelcome:<s:property value="#attr.msgWelcome"/><br>
#parameters.msgWelcome:<s:property value="#parameters.msgWelcome"/><br>
<hr>
使用OGNL访问值栈的root对象<br>
carUser.userId:<s:property value="carUser.userId"/> <br>
carUser.userName:<s:property value="carUser.userName"/> <br>
<hr>
carUsers[0].userId:<s:property value="carUsers[0].userId"/><br>
carUsers[0].userName:<s:property value="carUsers[0].userName"/><br>
carUserList[1].userId:<s:property value="carUserList[1].userId"/><br>
carUserList[1].userName:<s:property value="carUserList[1].userName"/><br>
carUserMap['third'].userId:<s:property value="carUserMap['third'].userId"/><br>
carUserMap['third'].userName:<s:property value="carUserMap['third'].userName"/><br>
<hr>
数组元素投影carUsers.{userName}<br>
<s:iterator value="carUsers.{userName}">
<s:property />
</s:iterator>
<br>
List元素投影carUserList.{userName}<br>
<s:iterator value="carUserList.{userName}">
<s:property />
</s:iterator>
<br>
Map元素投影carUserMap.{userName}<br>
<s:iterator value="carUserMap.{userName}">
<s:property />
</s:iterator>
<hr>
对数组元素进行选择操作(carUsers.{?#userId > 1})<br>
<s:iterator value="carUsers.{?#this.userId>1}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对数组元素进行选择操作((carUsers.{^#userId > 1})<br>
<s:iterator value="carUsers.{^#this.userId>1}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对数组元素进行选择操作((carUsers.{$#userId > 1})<br>
<s:iterator value="carUsers.{$#this.userId>1}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<hr>
对List元素进行选择操作(carUserList.{?#userId > 4})<br>
<s:iterator value="carUserList.{?#this.userId>4}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对List元素进行选择操作(carUserList.{^#userId > 4})<br>
<s:iterator value="carUserList.{^#this.userId>4}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对List元素进行选择操作(carUserList.{$#userId > 4})<br>
<s:iterator value="carUserList.{$#this.userId>4}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<hr>
对Map元素进行选择操作(carUserMap.{?#userId > 7})<br>
<s:iterator value="carUserMap.{?#this.userId>7}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对Map元素进行选择操作(carUserMap.{^#userId > 7})<br>
<s:iterator value="carUserMap.{^#this.userId>7}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
对Map元素进行选择操作(carUserMap.{$#userId > 7})<br>
<s:iterator value="carUserMap.{$#this.userId>7}">
<s:property value="userId"/>:<s:property value="userName"/>
</s:iterator>
<br>
s:select name="car" list="carUserList.{userName}
<s:select name="car" list="carUserList.{userName}"></s:select>
<br>
s:checkboxlist list="carUserMap.{userName}" name="checkboxlist1"
<s:checkboxlist list="carUserMap.{userName}" name="checkboxlist1"></s:checkboxlist>
</body>
</html>
ognl.java
package hp.epm.ssh.app.ognl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import hp.epm.ssh.app.bean.CarUser;
public class OgnlAction {
private CarUser carUser;
private CarUser[] carUsers;
private List<CarUser> carUserList;
private Map<String, CarUser> carUserMap;
public CarUser getCarUser() {
return carUser;
}
public void setCarUser(CarUser carUser) {
this.carUser = carUser;
}
public CarUser[] getCarUsers() {
return carUsers;
}
public void setCarUsers(CarUser[] carUsers) {
this.carUsers = carUsers;
}
public List<CarUser> getCarUserList() {
return carUserList;
}
public void setCarUserList(List<CarUser> carUserList) {
this.carUserList = carUserList;
}
public Map<String, CarUser> getCarUserMap() {
return carUserMap;
}
public void setCarUserMap(Map<String, CarUser> carUserMap) {
this.carUserMap = carUserMap;
}
@SuppressWarnings("unchecked")
public String execute(){
ActionContext.getContext().getApplication().put("msgWelcome", "欢迎登陆系统(application)");
ActionContext.getContext().getSession().put("msgWelcome", "欢迎登陆系统(session)");
((Map<String, Object>)(ActionContext.getContext().get("request"))).put("msgWelcome", "欢迎登陆系统(request)");
ActionContext.getContext().put("msgWelcome1", "欢迎登陆系统(request)");
carUser=createCarUser(1, "刘一");
carUsers=new CarUser[3];
carUsers[0]=createCarUser(2, "陈二");
carUsers[1]=createCarUser(3, "张三");
carUsers[2]=createCarUser(4, "李四");
carUserList=new ArrayList<CarUser>();
carUserList.add(createCarUser(5, "王五"));
carUserList.add(createCarUser(6, "赵六"));
carUserList.add(createCarUser(7, "孙七"));
carUserMap=new HashMap<String, CarUser>();
carUserMap.put("first", createCarUser(8, "周八"));
carUserMap.put("second", createCarUser(9, "吴九"));
carUserMap.put("third", createCarUser(10, "郑十"));
return "success";
}
private CarUser createCarUser(Integer userId,String userName){
CarUser carUser=new CarUser();
carUser.setUserId(userId);
carUser.setUserName(userName);
return carUser;
}
}
struts.xml
</action>
<action name="ognlAction" class="hp.epm.ssh.app.ognl.OgnlAction" >
<result name="success">/WEB-INF/Ognl/ognlShow.jsp</result>
</action>