struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
有两种方式可以实现在Action中使用Servlet的API。一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony.xwork2.ActionContext类。
struts2推荐的是使用第二种方式:使用ActionContext类来对request、session和application对象进行操作。
方式一:使用ServletActionContext类(紧耦合)
1. 创建控制层
package com.clzhang.struts2.demo4; import org.apache.struts2.ServletActionContext; public class ServletActionContextTestAction {
private String usernameRequest;
private String usernameSession;
private String usernameApplication; public String getUsernameApplication() {
return usernameApplication;
} public String getUsernameRequest() {
return usernameRequest;
} public void setUsernameRequest(String usernameRequest) {
this.usernameRequest = usernameRequest;
} public String getUsernameSession() {
return usernameSession;
} public void setUsernameSession(String usernameSession) {
this.usernameSession = usernameSession;
} public void setUsernameApplication(String usernameApplication) {
this.usernameApplication = usernameApplication;
} public String execute() {
usernameRequest = "request级别的用户名:张三";
usernameSession = "session级别的用户名:李四";
usernameApplication = "application级别的用户名:赵五"; ServletActionContext.getRequest().setAttribute("usernameRequest",
usernameRequest);
ServletActionContext.getRequest().getSession().setAttribute(
"usernameSession", usernameSession);
ServletActionContext.getServletContext().setAttribute(
"usernameApplication", usernameApplication); return "showResult";
}
}
2. 修改配置文件struts.xml
<action name="servletActionContextTest" class="com.clzhang.struts2.demo4.ServletActionContextTestAction">
<result name="showResult">/struts2/demo4/showResult.jsp</result>
</action>
3. 创建显示JSP文件(showResult.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
#request.usernameRequest值是:
<s:property value="#request.usernameRequest" />
<br />
#session.usernameSession值是:
<s:property value="#session.usernameSession" />
<br />
#application.usernameApplication值是:
<s:property value="#application.usernameApplication" />
<br />
</body>
</html>
4. 测试
打开IE,输入地址:http://127.0.0.1:8080/st/struts2/servletActionContextTest.action
结果如下:
方式二:使用ActionContext类(松耦合)
1. 创建控制层
package com.clzhang.struts2.demo4; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class ActionContextTestAction { public String execute() { // 第一种方法向request对象中放数据
// 因为ActionContext类没有getRequest()这样的方法,所以需要使用下面的方式获取request对象
Map request = (Map) ActionContext.getContext().get("request");
request.put("requestValue", "this is reqeustValue"); // 第二种方法向request对象中放数据
ActionContext.getContext().put("otherrequest",
"this is otherrequest value"); Map session = (Map) ActionContext.getContext().getSession();
session.put("sessionValue", "this is sessionValue"); Map application = (Map) ActionContext.getContext().getApplication();
application.put("applicationValue", "this is applicationValue"); return "showScopeValue";
}
}
2. 修改配置文件struts.xml
<action name="actionContextTest" class="com.clzhang.struts2.demo4.ActionContextTestAction">
<result name="showScopeValue">/struts2/demo4/showScopeValue.jsp</result>
</action>
3. 创建显示JSP文件(showScopeValue.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
request:
<s:property value="#request.requestValue" />
<br />
<br />
otherrequest:
<s:property value="#request.otherrequest" />
<br />
<br />
session:
<s:property value="#session.sessionValue" />
<br />
<br />
application:
<s:property value="#application.applicationValue" />
<br />
<br />
</body>
</html>
4. 测试
打开IE,输入地址:http://127.0.0.1:8080/st/struts2/actionContextTest.action
结果如下:
struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性的更多相关文章
- jsp中的四个作用域,九个内置对象分别是什么?
九大内置对象: 内置对象(又叫隐含对象),就是在jsp中,不需要创建(由服务器<容器>来创建),可以直接使用的对象. 对象 含义 类 作用域 request 请求对象 类型 javax.s ...
- JS中的常量(基本数据类型)和内置对象
JS中的基本数据类型: String .number.null.boolean.undefined.object.symbol(ES6) 1. 利用typeof运算符时其中只有null是异常的,ty ...
- Struts2框架中使用Servlet的API示例
1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...
- 在Struts2框架中使用Servlet的API
1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...
- JSP 脚本中的 9 个内置对象
JSP 脚本中包含了 9 个内置对象,这 9 个内置对象都是 Servlet API 接口的实例,只是 JSP 规范对它们进行了默认初始化. 这 9 个内置对象如下: 1.application:ja ...
- JSP中的Java代码和内置对象
一.JSP中的Java代码 (一)JSP页面中有三种方式嵌入java代码: 1.java的表达式 格式:<%= java表达式 %> 2.java的语句 格式:<% java语句&g ...
- JS中一些常用的内置对象
在JS中,经常会遇到明明知道一个对象有某个属性或方法,可是又不知道怎么写的情况.下面,我就罗列了一些JS中常用的内置对象的属性和方法. Math对象: Math对象的作用是执行常见的算术任务. 首先M ...
- 在Action中获取servlet API
Struts2的Action组件是不依赖servlet API 的.那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响 ...
- 在struts2的action中操作域对象(request、session)
在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...
随机推荐
- Python两个变量的值进行交换的方法
Python两个变量的值进行交换的方法 使用第三方变量: '''这是第一种赋值方法,采用第三方变量''' c = a a = b b = c 使用两个变量: '''使用两个变量''' a = a+b ...
- Flex报错Error #2048: 安全沙箱冲突
Flex+JPA架构,JPA程序迁移,从Aserver到B. 其它一切没变.唯一变的就是IP. 前端Flex也就是swf报错Error #2048: 安全沙箱冲突:http://xxx.swf 不能从 ...
- Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 20160210.CCPP体系具体解释(0020天)
程序片段(01):01.二级指针.c 内容概要:二级指针 #include <stdio.h> #include <stdlib.h> //01.二级指针: // 1.使用场景 ...
- Axure 图片轮播(广告通栏图片自动播放效果)
baiduYunpan:http://pan.baidu.com/s/1eRPCy90 里面的“图片轮播”部件即可实现这个功能
- python之模块poplib之常见用法
# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始 ...
- CentOS7下HTTP并发测试工具Apache Benchmark(AB)安装和使用
安装: yum -y install httpd-tools 使用: ab -c -n http://10.255.67.60:1111/info -c 并发数,concurrency -n 发送多少 ...
- DUBBO本地搭建及小案例 (转)
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- 批处理转exe工具(Quick Batch File Compiler )|bat格式化exe
看到的,就是回忆.历史总是那么漫不经心,走完一生.留下可以记忆的脚本.... 对于window编写的bat脚本,想加密吗? 你所想的,前辈们基本上都有产出成果.所以在这个开源.共享.进步的互联网时代. ...
- 下载完整版Chrome离线安装文件的官方地址
只在自己账号下安装Download Google Chrome Standalone Offline Installer (32-bit) http://www.google.com/chrome/ ...