Struts2框架06 ValueStack
原文地址:点击前往
1 什么是ValueStack
称为值栈,Struts提供的共享数据的数据结构
2 为什么要使用ValueStack
从控制器向浏览器传递数据
存储与请求相关的对象信息(session/application)
3 ValueStack对象的生命周期
请求进入到服务器端后,在内存中就会传创建一个ValueStack对象;当请求处理结束以后,ValueStack对象就会被清除
4 如何访问ValueStack中的数据
利用OGNL表达式获取
利用EL表达式获取
5 在ValueStack中存储数据的区域划分
Contents (栈结构) 利用OGNL或者EL来获取数据
Context (Map结构) 利用 #key 来获取数据
7 案例:从控制器向浏览器传值,展示valueStack区域
7.1 导包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiangxu</groupId>
<artifactId>ssh03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
</project>
pom.xml
7.2 配置文件
7.2.1 spring_context.xml
配置注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 配置组件扫描 -->
<context:component-scan base-package="cn.xiangxu" /> </beans>
spring_context.xml
7.2.2 struts.xml
配置访问路径、访问网名、action处理类
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 测试struts整合spring时用 -->
<package name="test" namespace="/test" extends="json-default">
<action name="demo">
<result>
/WEB-INF/jsp/msg.jsp
</result>
</action>
</package> <package name="vs" namespace="/vs" extends="json-default">
<action name="valueStack" class="valueStackAction" method="valueStaceMethod">
<result name="success">
/WEB-INF/jsp/valueStack.jsp
</result>
</action>
</package> </struts>
struts.xml
7.2.3 web.xml
配置spring监听器
配置spring配置文件位置
配置主控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ssh03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置spring监听
目的:容器启动时自动加载一些东西到缓存中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_*.xml</param-value>
</context-param> <!-- 配置主控制器和过滤条件 -->
<filter>
<filter-name>mvc</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>mvc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
web.xml
7.3 编写action处理类
package cn.xiangxu.action; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack; import cn.xiangxu.entity.Person; @Controller
@Scope("prototype")
public class ValueStackAction { private String message; public String valueStaceMethod() {
System.out.println("跟valueStack相关的action类"); message = "我是控制类中的属性message"; // 利用工厂方法来获取session对象时就使用下面两行代码
ActionContext context = ActionContext.getContext();
context.getSession().put("loginName", "warrior"); // 向session中插入数据 context.getSession().put("password", "123456"); // 向session中插入数据 // 利用上下文对象来获取ValueStack对象
ValueStack valueStack = context.getValueStack(); Person person = new Person();
person.setId("333");
person.setName("fury");
person.setMessage("hello fury");
valueStack.push(person); // 将数据插入到对象栈中 return "success";
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} }
ValueStackAction.java
在控制类中需要用到的实体类
package cn.xiangxu.entity;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -7221161390673280278L;
private String id;
private String name;
private String message;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String id, String name, String message) {
super();
this.id = id;
this.name = name;
this.message = message;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", message=" + message + "]";
}
}
Person.java
7.4 编写jsp页面
7.4.1 利用EL表达式访问ValueStack中的数据的格式
${变量名}
7.4.2 利用OGNL表达式访问ValueStack中的数据的格式
<s:property value="变量名"/>
<s:property value="#session.变量名"/>
注意:为什么访问sesseion中的数据时需要在前面加 #session. 是因为....【自己百度去,或者参见本博客顶端的连接;三少能力有限,讲不清楚】
注意:在读取栈结构中的数据时是从栈顶开始读的,如果有两个变量的名字相同,那么读取到的只会是相对前面的那个变量的值
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%> <!-- 引入struts2标签库 -->
<%@ taglib prefix="s" uri="/struts-tags" %> <!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">
<title>Insert title here</title>
</head>
<body>
<h2>跟valueStack有关的页面</h2>
<hr /><hr /> <h2>利用EL表达式从valuesStack中获取数据</h2>
<h3>${message }</h3>
<hr />
<h3>${loginName }</h3>
<hr />
<h3>${password }</h3>
<hr /><hr /> <h2>利用OGNL表达式获取valueStack中的数据</h2>
<h3><s:property value="message"/></h3>
<hr />
<h3><s:property value="#session.loginName"/></h3>
<hr />
<h3><s:property value="#session.password"/></h3> <hr /><hr /> <s:debug></s:debug>
</body>
</html>
valueStack.jsp
7.5 项目结构图

Struts2框架06 ValueStack的更多相关文章
- Struts2框架基础概念总结
一.struts2框架 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...
- Struts2 框架的值栈
1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...
- 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .
笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 声明:本文参考Strut ...
- Struts2 框架验证
struts2框架验证(xml方式): * 首先要从页面中获取对应的标签name属性的值,在动作类action中声明同名的属性,提供get和set方法 * 创建一个xml格式验证文 ...
- Struts2框架学习(三) 数据处理
Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...
- struts2框架概述
框架概述 什么是框架,为什么使用框架,框架优点 框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题 框架,即framework.其实就是某种应用的半成品,就是一组组件,供你 ...
- [ SSH框架 ] Struts2框架学习之四(自定义拦截器)
一.Struts2的拦截器 1.1 拦截器概述 拦截器,在AOP( Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截 ...
- struts2框架学习之第三天
day03 上传下载 1 上传下载组件介绍 l jspSmartUpload(model1的年代): l apache-commons-fileupload,Struts2默认上传组 ...
- struts2框架
详细教程 参考struts教程https://www.w3cschool.cn/struts_2/struts_configuration.html Struts2 基于MVC设计模式的web应用程序 ...
随机推荐
- 51nod 1099 贪心/思维
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 1099 任务执行顺序 基准时间限制:1 秒 空间限制:13107 ...
- this license has been cancelled
是因为IDEA注册码的问题, 解决方案: 修改此路径的hosts文件:C:\Windows\System32\drivers\etc\hosts 在其最后一行加入:“0.0.0.0 account.j ...
- Windows7+VS2008 下编译Subversion 1.8.3
一.需要的软件包 1.python-2.7.5.msi http://www.python.org/ 2.ActivePerl-5.8.8.822-MSWin32-x86-280952.msi h ...
- mysql 视图、触发器、事物、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- 在Java中定义常量
方法一采用接口(Interface)的中变量默认为static final的特性. 方法二采用了Java 5.0中引入的Enum类型. 方法三采用了在普通类中使用static final修饰变量的方法 ...
- bgcolor RGB 和16进制之间的转换,16进制转RGB,源码
<p>bgcolor RGB 和16进制之间的转换,16进制转RGB,源码例如:<br /> 输入 201,255,201 转换成 #C9FFC9</p> < ...
- Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of th ...
- element-ui table 底部滚动条问题
1.将 .el-table 标签css属性中的 position: relative; width: 100%; max-width: 100%; 修改成 position: absolute; wi ...
- Linux动态gif图的录制
Linux动态gif图的录制 Linux动态gif图的录制 byzanz的安装与使用 recordmydesktop再convert成gif 参考资料 前几天写了两篇博客vim的配置和Vim的自动代码 ...
- 基于dubbo的SOA项目改造
工程改造: 1.原来工程删除掉表现层的模块,将表现层独立出来. 2.将原来的工程改造.将service的打包方式改为改为war. 3.在service模块中添加web.xml文件 4.在web.xml ...