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 ...
随机推荐
- Java生成XML文件与XML文件的写入
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6651643.html 既然能在代码中解析XML文档获取数据,当然也能通过代码动态生成XML文档了. 与解析X ...
- python模块之linecache
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之linecache import linecache ''' >>> h ...
- 对TCP连接被重置解决方案的探究
分类: 网络与安全 对TCP连接被重置解决方案的探究——跨过GFW通向自由网络的可行途径 2010年05月25日 星期二 上午 00:19 这个标题有点长——其实开始只想写破折号之前的部分,因为这种技 ...
- 通过#define连接字符串的特殊方法[转]
//在#define中,标准只定义了#和##两种操作.#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串. #define Conn(x,y) x##y //连接 ...
- 在执行context.getContentResolver.query()方法时出现错误。
1. 在执行context.getContentResolver.query()方法时出现错误. 07-15 18:46:13.470: E/AndroidRuntime(13624): FATAL ...
- ios中UIWebview中加载本地文件
[super viewDidLoad]; webview=[[UIWebView alloc] initWithFrame:self.view.bounds]; [self.view addSubvi ...
- fileUpload(草稿)
Java关于文件上传的一个例子 发表于2012/6/7 13:01:56 1374人阅读 分类: JavaWeb 文件上传不能用get方式提交,因为他提交的数据量最多只有1kb, IE浏览器默认情况 ...
- apache2.2 虚拟主机配置(转)
转自:http://blog.csdn.net/zm2714/article/details/8351342 一.改动httpd.conf 打开appserv的安装文件夹,找到httpd.conf文件 ...
- java实现simhash算法
一个牛人分享的,放在github上,用java实现,网络上还有很多用ruby写的 https://github.com/commoncrawl/commoncrawl/blob/master/sr ...
- Ubuntu中安装配置和卸载FTP(转)
转载:http://zyjustin9.iteye.com/blog/2178943 一.安装1.用apt-get工具安装vsftpd: $ sudo apt-get install vsftpd 2 ...