有两种方式可以实现在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,设置、读取各种内置对象的属性的更多相关文章

  1. jsp中的四个作用域,九个内置对象分别是什么?

    九大内置对象: 内置对象(又叫隐含对象),就是在jsp中,不需要创建(由服务器<容器>来创建),可以直接使用的对象. 对象 含义 类 作用域 request 请求对象 类型 javax.s ...

  2. JS中的常量(基本数据类型)和内置对象

    JS中的基本数据类型: String .number.null.boolean.undefined.object.symbol(ES6) 1.  利用typeof运算符时其中只有null是异常的,ty ...

  3. Struts2框架中使用Servlet的API示例

    1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...

  4. 在Struts2框架中使用Servlet的API

    1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...

  5. JSP 脚本中的 9 个内置对象

    JSP 脚本中包含了 9 个内置对象,这 9 个内置对象都是 Servlet API 接口的实例,只是 JSP 规范对它们进行了默认初始化. 这 9 个内置对象如下: 1.application:ja ...

  6. JSP中的Java代码和内置对象

    一.JSP中的Java代码 (一)JSP页面中有三种方式嵌入java代码: 1.java的表达式 格式:<%= java表达式 %> 2.java的语句 格式:<% java语句&g ...

  7. JS中一些常用的内置对象

    在JS中,经常会遇到明明知道一个对象有某个属性或方法,可是又不知道怎么写的情况.下面,我就罗列了一些JS中常用的内置对象的属性和方法. Math对象: Math对象的作用是执行常见的算术任务. 首先M ...

  8. 在Action中获取servlet API

    Struts2的Action组件是不依赖servlet API 的.那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响 ...

  9. 在struts2的action中操作域对象(request、session)

    在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...

随机推荐

  1. Aerospike系列:2:商业版和社区版的比较

    http://www.aerospike.com/products-services

  2. C# 将一个string数组转换为int数组

    int[] channelCIdArr = Array.ConvertAll(channelIdStr.Split(','),s=>int.Parse(s));

  3. 让span对宽度有响应而且兼容多种浏览器

    span {display:-moz-inline-box; display:inline-block; width:20px;height:20px;}

  4. 写带有清晰图片的博客:如何将word中的图片复制到windows live writer保持大小不变--清晰度不变

    写blog的习惯,先在word写了,复制到windows live writer,再发布到博客园.word中的文章,图片有缩放比例,复制到windows live writer后图片变得不清晰.除了一 ...

  5. [解决思路]ORA-01078: failure in processing system parameters LRM-00109: could not open parameter file

    oracle数据库,服务器异常断电,导致数据库不能启动.... 错误提示: SQL> startup ORA-01078: failure in processing system parame ...

  6. 加载ConversationListActivity以及延迟的使用

    1. 加载会话列表分两步加载. 且第1步和第2步之间有些延迟,便于第1步的结果及时在ListView中显示出来. mHandler.postDelayed(new LoadThreadsExtra() ...

  7. 类型“System.Data.SQLite.SQLiteParameter”在未被引用的程序集中定义。必须添加对程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”的引用

    出现这个问题是 你的系统是64位 同时 dll生成的 any cpu 应该换为 x86

  8. iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果

    一.需求 实现imageView的缩放旋转效果,一般有两种方式: 1.底层加scrollview,利用scrollview的属性实现.(推荐这种,这是我比较后发现的,手势做缩放旋转会有点弊端) 2.利 ...

  9. Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错

    @Repository public class SysUerCDAO { @Autowired private Hibernate_Credit hibernate_credit; /** * 根据 ...

  10. IKE 协议(转)

    from: http://lulu1101.blog.51cto.com/4455468/817872 IKE 协议 2012-03-26 21:49:50 标签:休闲 ike 职场 IKE 协议简介 ...