1.在 Struts2 中,根对象就是 ValueStack。在 Struts2 的任何流程当中,ValueStack 中的最顶层对象一定是 Action对象。所以可以直接取Action的参数
2. parameters,#parameters.username
request, #request.username
session, #session.username
application, #application.username
attr, #attr.username
以上几个对象叫做“命名对象”。
3. ValueStack与命名对象的关系:ValueStack是根对象,命名对象为非根对象需要在前面加#
4. 在Struts2的标签库处理类中将%{}所包含的字符串当成ognl表达式来处理,如href等标签直接将属性当成普通字符串来处理,所以需要在参数前家%{}
关于Struts2标签库属性值的%与#号的关系:
A. 如果标签的属性值是 OGNL表达式,那么无需加上%{}。
B. 如果标签的属性值是字符串类型,那么在字符串当中凡是出现
的%{}都会被解析成 OGNL 表达式,解析完毕后再与其他的字符串进
行拼接构造出最后的字符串值。
C. 我们可以在所有的属性值上加%{},这样如果该属性值是 OGNL 表达式,那么标签处理类就会将%{}忽略掉。
Jsp demo:
<%@ page language="java" import="java.util.*, com.opensymphony.xwork2.*, com.opensymphony.xwork2.util.*, com.shengsiyuan.action.ognl.*" 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 ‘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>
username: <s:property value="username"/><br>
password: <s:property value="password"/><br>
----------------------------------------<br>
username: <s:property value="#parameters.username"/><br>
password: <s:property value="#parameters.password"/><br>
----------------------------------------<br>
request: <s:property value="#request.hello"/><br>
session: <s:property value="#session.hello"/><br>
application: <s:property value="#application.hello"/><br>
attr: <s:property value="#attr.hello"/><br>
----------------------------------------<br>
request: <%= ((Map)ActionContext.getContext().get("request")).get("hello") %><br>
session: <%= ActionContext.getContext().getSession().get("hello") %><br>
application: <%= ActionContext.getContext().getApplication().get("hello") %><br>
attr: <%= ((Map)ActionContext.getContext().get("attr")).get("hello") %><br>
----------------------------------------<br>
person1: address: <s:property value="list[0].address"/><br>
person2: age: <s:property value="list[1].age"/><br>
person1: cat1: name: <s:property value="list[0].cat.name"/><br>
size: <s:property value="list.size"/><br>
isEmpty: <s:property value="list.isEmpty()"/><br>
----------------------------------------<br>
person1: address: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(0).getAddress() %><br>
person2: age: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(1).getAge() %><br>
person1: cat1: name: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(0).getCat().getName() %><br>
----------------------------------------<br>
person2:friend: <s:property value="list[1].friends[2]"/><br>
person2:frined: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(1).getFriends()[2] %><br>
----------------------------------------<br>
person2: map2: <s:property value="list[1].map[‘hello2‘]"/><br>
----------------------------------------<br>
filtering: <s:property value="list.{? #this.name.length() > 2}[1].name"/><br>
----------------------------------------<br>
<s:iterator value="list.{? #this.name.length() > 2}">
<s:property value="name"/><br>
<s:property value="cat.color"/><br>
<s:property value="friends[0]"/><br>
</s:iterator>
----------------------------------------<br>
projection: <br>
<s:iterator value="list.{age}">
<s:property/><br>
</s:iterator>
</body>
</html>
ActionDemo
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware
{
private String username;
private String password;
private Map<String, Object> requestMap;
private Map<String, Object> sessionMap;
private Map<String, Object> applicationMap;
private List<Person> list;
public List<Person> getList()
{
return list;
}
public void setList(List<Person> list)
{
this.list = list;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Override
public void setRequest(Map<String, Object> arg0)
{
System.out.println("setRequest invoked");
this.requestMap = arg0;
}
@Override
public void setSession(Map<String, Object> arg0)
{
System.out.println("setSession invoked!");
this.sessionMap = arg0;
}
@Override
public void setApplication(Map<String, Object> arg0)
{
this.applicationMap = arg0;
}
@Override
public String execute() throws Exception
{
Thread.sleep(20000);
requestMap.put("hello", "world");
sessionMap.put("hello","hello");
applicationMap.put("hello", "hello world");
Cat cat1 = new Cat("cat1", 20, "red");
Cat cat2 = new Cat("cat2", 30, "blue");
String[] friends1 = {"test1", "test2", "test3"};
String[] friends2 = {"welcome1", "welcome2", "welcome3"};
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
map1.put("test1", "test1");
map1.put("test2", "test2");
map2.put("hello1", "hello1");
map2.put("hello2", "hello2");
Person person1 = new Person("zhangsan", 20, "beijing", friends1, cat1, map1);
Person person2 = new Person("lisi", 30, "shanghai", friends2, cat2, map2);
list = new ArrayList<Person>();
list.add(person1);
list.add(person2);
return SUCCESS;
}
}
Struts2复习之OGNL(2),布布扣,bubuko.com
原文:http://blog.csdn.net/caicongyang/article/details/19934809