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 ...
随机推荐
- 开启mysql远程登录
开发过程中经常遇到远程访问mysql的问题,每次都需要搜索,感觉太麻烦,这里记录下,也方便我以后查阅. 首先访问本机的mysql(用ssh登录终端,输入如下命令): mysql -uroot -p 输 ...
- itextpdf 备忘
加删除线: .setUnderline(Color.BLACK, 2.0f, 0.0f, 6.0f, 0.0f, 1) https://developers.itextpdf.com/examples ...
- HTML拾遗
一:标签 1:强调 <strong>加醋.<em>斜体 2:单独样式 <span>如果不加样式,那它包围的文字就是普通文字,可以在span中增加样式,就所包围的内容 ...
- django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)
from django.http import HttpResponse def download_file(request): # Text file #response = HttpRespons ...
- 转 php安装错误configure: error: Please reinstall the libcurl distribu
今天配置一台server的php支持curl的时候, 出现如下报错 checking for cURL in default path... not foundconfigure: error: Pl ...
- β particle, α particle, γ ray, ionization chamber
Alpha particles consist of two protons and two neutrons bound together into a particle identical to ...
- 转:CMake快速入门教程-实战
CMake快速入门教程:实战 收藏人:londonKu 2012-05-07 | 阅:10128 转:34 | 来源 | 分享 0. 前言一个多月 ...
- Xcode使用小技巧-filter查找功能和查看最近修改的文件
今天偶然发现了关于Xcode的一个小技巧: 1.查看最近修改的文件 2.使用filter查找制定文件 没错,就是下面这个东西,很容易忽略的一个小工具,在Xcode左下角位置. 通过这个,我们能够在整个 ...
- linux shell 脚本攻略学习15--如何只列出目录,如何快速切换目录
工作中经常遇到关于目录方面的问题,例如,如何只列出当前目录下的所有目录,以及如何快速高效的切换目录,而不需要使用鼠标,下面将简单介绍关于这两方面的解决方案: 一.如何只列出目录? 看似简单的任务,其实 ...
- mysql5.7创建用户授权删除用户撤销授权
一, 创建用户: 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - ...