context map的概念介绍:
每次请求时都会创建一个context map的对象, value stack和context map的数据是可以相互转换的
key | value | 说明 |
value stack | List集合 | 以栈的方式来存储 |
request | Map<String,Object>结构 | 以键值对的方式存储请求范围的数据 |
session | Map<String,Object>结构 | 以键值对的方式存储会话范围的数据 |
application | Map<String,Object>结构 | 以键值对的方式存储应用范围的数据 |
action | Object类型 | 当前访问的动作类对象 |
parameters | Map<String,Object>结构 | 存储请求的参数 |
attr | Map<String,Object>结构 | 根据key从page/request/session/application范围依次查找属性的值 |
我们存取的数据采用两种结构, 一种是valueStack(值栈), 采用set<map>的形式, 另一种是contextMap(map)的形式
在contextMap中存取数据:
在请求范围内存取数据:
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<!-- 需要使用#key的方式来取数据
-->
<s:property value="#name"/>
<s:debug></s:debug>
</body>
</html>
动作类代码:
package com.rl.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class PersonAction extends ActionSupport {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
System.out.println(username);
//获取动作类的上下文ActionContext(包含了contextMap和valueStack)
ActionContext context = ServletActionContext.getContext();
//在contextMap中存储数据, 默认相当于存储在request(并非存储在request中, 但是生命周期跟request一样, 就是一次请求)
context.put("name", "zhangsan");
return super.execute();
}
}
struts.xml的配置我就不贴上来了.
debug展示:
从图中可以看出数据并非存储在request中, 但是生命周期跟request一样(在一次请求的范围内)
在会话范围内存取数据:
动作类部分代码:
@Override
public String execute() throws Exception {
System.out.println(username);
//获取动作类的上下文ActionContext(包含了contextMap和valueStack)
ActionContext context = ServletActionContext.getContext();
//在contextMap中存储数据, 默认相当于存储在request(并非存储在request中, 但是生命周期跟request一样, 就是一次请求)
context.put("name", "zhangsan");
//获取session
Map<String, Object> session = context.getSession();
//将数据存储在session中
session.put("user", "李四");
return super.execute();
}
success.jsp部分代码:
<body>
<!-- 需要使用#key的方式来取数据
-->
<!-- 在请求的范围内 -->
<s:property value="#name"/>
<!-- 在会话的范围内 -->
<s:property value="#session.user"/>
<s:debug></s:debug>
</body>
debug展示:
访问结果:
从图中可以看出, 跨在同一个session内是可以拿到到"user"的值, 但是拿不到"name"的值
在application范围内存储数据:
动作类部分代码:
//获取application
Map<String, Object> application = context.getApplication();
//将数据存储在application中
application.put("pv", "12345");
success.jsp部分代码:
<!-- 在应用范围内 -->
<s:property value="#application.pv"/><br>
只有服务器不关闭, application范围内都能去到该数据.
在valueStack(值栈)中存取数据:
请求的参数会存储在值栈中, 也可以手动存储
自动存储:
动作类部分代码:
public String execute() throws Exception {
System.out.println(username);
return super.execute();
}
success.jsp部分代码:
<s:debug></s:debug>
<hr>
<!-- 在值栈中取值直接使用变量名即可 -->
<s:property value="username"/>
debug展示:
从上图可以看出, 此时的"username"的值存储于valueStack(值栈)中
新建一个model类Person:
package com.rl.model;
public class Person {
private int personId;
private String personName;
private int gender;
public Person() {
}
public Person(int personId, String personName, int gender) {
super();
this.personId = personId;
this.personName = personName;
this.gender = gender;
}
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
success.jsp部分代码:
<s:property value="personId"/><br>
<s:property value="personName"/><br>
<s:property value="gender"/><br>
<hr><br>第二种从valueStack中取值的方法<br>
<s:property value="person.personId"/><br>
<s:property value="person.personName"/><br>
<s:property value="person.gender"/><br>
debug展示:
访问结果:
手动存储(一般都是手动放在contextMap中):
动作类部分代码:
public String execute() throws Exception {
ActionContext context = ServletActionContext.getContext();
Person person1 = new Person(2, "王五", 2);
//获取值栈
ValueStack stack = context.getValueStack();
//手动压栈
stack.push(person1);
return super.execute();
}
success.jsp部分代码:
<s:property value="personId"/><br>
<s:property value="personName"/><br>
<s:property value="gender"/><br>
<hr>
<!-- 取栈中的值 -->
<s:property value="[1].personId"/><br>
<s:property value="[1].personName"/><br>
<s:property value="[1].gender"/><br>
<hr>
<s:property value="person.personId"/><br>
<s:property value="person.personName"/><br>
<s:property value="person.gender"/><br>
debug展示:
访问结果: