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+"/";
%>
My JSP 'ognl.jsp' starting page
username:
password:
----------------------------------------
username:
password:
----------------------------------------
request:
session:
application:
attr:
----------------------------------------
request: <%= ((Map)ActionContext.getContext().get("request")).get("hello") %>
session: <%= ActionContext.getContext().getSession().get("hello") %>
application: <%= ActionContext.getContext().getApplication().get("hello") %>
attr: <%= ((Map)ActionContext.getContext().get("attr")).get("hello") %>
----------------------------------------
person1: address:
person2: age:
person1: cat1: name:
size:
isEmpty:
----------------------------------------
person1: address: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(0).getAddress() %>
person2: age: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(1).getAge() %>
person1: cat1: name: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(0).getCat().getName() %>
----------------------------------------
person2:friend:
person2:frined: <%= ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getList().get(1).getFriends()[2] %>
----------------------------------------
person2: map2:
----------------------------------------
filtering:
----------------------------------------
----------------------------------------
projection:
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
requestMap;
private Map
sessionMap; private Map
applicationMap; private List
list; public List
getList() { return list; } public void setList(List
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
arg0) { System.out.println("setRequest invoked"); this.requestMap = arg0; } @Override public void setSession(Map
arg0) { System.out.println("setSession invoked!"); this.sessionMap = arg0; } @Override public void setApplication(Map
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
map1 = new HashMap
(); Map
map2 = new HashMap
(); map1.put("test1", "test1"); map1.put("test2", "test2"); map2.put("hello1", "hello1"); map2.put("he