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应用程序 ...
随机推荐
- Linux_总结_01_VMware14虚拟机下安装CentOS7.x
一.前言 本文是采用最小安装方式 二.下载 1.官网 https://www.centos.org/download/ 在此页面下,选择 Minimal ISO 进行下载. 三.VMWare中新建虚拟 ...
- Oracle 11g 客户端连接 oracle 10g 服务端,乱码问题
从网上搜索资料基本确定:字符集错误 Pl/sql 连接到oracle 数据库 “select userenv('language') from dual” 找到服务端的对应的字符集,拷贝之: 到本 ...
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- poj2263 zoj1952 Heavy Cargo(floyd||spfa)
这道题数据范围小,方法比较多.我用floyd和spfa分别写了一下,spfa明显有时间优势. 一个小技巧在于:把城市名称对应到数字序号,处理是用数字. 方法一:spfa #include<ios ...
- CANopenSocket CANopenCGI.c hacking
/**************************************************************************************** * CANopenS ...
- HihoCoder1070 区间最小值(简单线段树)
个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量weigh ...
- 系列文章--Silverlight与WCF通信
Silverlight与WCF通信(一) :Silverlight通过httpBinding访问IIS宿主WCF 摘要: 首语本人在学习Silverlight 和 WCF的时候,各种问题层出不穷,在园 ...
- 异常:java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlType
这个是jdK版本的问题的. 本地编译的jar包是1.8的,但是跑jar包的环境jdk版本是1.9的. 升级1.9之后由于jdk当方面的取消了几个jar,所以导致编译起不来. 明天研究一下如何添加jar ...
- WCF svcutil工具
通过SvcUtil.exe生成客户端代码和配置 WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务 ...
- mongoTemplate操作内嵌文档
关系型数据库中,表与表的关联关系有1:1,也有1:n的.在java的面向对象的世界里就是主对象嵌子对象,与主对象嵌集合<子对象>的两种形式. 主对象嵌子对象操作: 新增.修改都直接用如下方 ...