1.struts作为控制器,正常非常多时候要訪问到servlet的API。经常使用功能:

  (1).获取请求參数,控制界面跳转

  (2).把共享数据存储于request,session,servletContext中,获取作用域中的数据

宏观的来说,应该有三种訪问方式。

2.第一种:实现接口,訪问Action时完毕注入

ServletContextAware

void setServletContext(javax.servlet.ServletContext context)



ServletRequestAware

void setServletRequest(javax.servlet.http.HttpServletRequest request)



ServletResponseAware

void setServletResponse(javax.servlet.http.HttpServletResponse response)



上述方式:Action和ServletAPI耦合太深了.

简单的演示样例代码:

package cn.wwh.www.web.servletapi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 上午07:54:05
*/
public class ParamAction1 extends ActionSupport implements ServletRequestAware,
ServletResponseAware { private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpServletResponse response; @Override
public String execute() throws Exception {
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println("name:" + name);
System.out.println("age:" + age);
response.getWriter().write(name + "<br/>");
response.getWriter().write(age);
// 没有起到效果,非常奇怪
request.getRequestDispatcher("/views/servletapi/result.jsp").forward(
request, response);
return Action.NONE;
} public void setServletRequest(HttpServletRequest request) {
this.request = request;
} public void setServletResponse(HttpServletResponse response) {
this.response = response; } }


3.另外一种:使用ServletActionContext(开发中使用的非常多,由于简单,直观)ServletActionContext: 通过该类提供了ServletAPI的环境,能够获取到Servlet的API信息static PageContext getPageContext()static HttpServletRequest getRequest()static HttpServletResponse getResponse()static
ServletContext getServletContext()

该方案可避免Action类实现XxxAware接口。但Action依旧与Servlet API直接耦合可是该方式和ServletApi也有耦合.

简单的实例代码:

package cn.wwh.www.web.servletapi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 上午09:09:02
*/
public class ParamAction2 extends ActionSupport { private static final long serialVersionUID = 1L;
public String execute() throws Exception { HttpServletRequest req = ServletActionContext.getRequest();
String name = ServletActionContext.getRequest().getParameter("name");
String age = ServletActionContext.getRequest().getParameter("age");
HttpSession session = req.getSession();
session.getServletContext();
System.out.println(name);
System.out.println(age);
HttpServletResponse resp = ServletActionContext.getResponse();
return NONE;
} }

4.第三种方式:使用ActionContext类(没有和ServletApi耦合,开发推荐使用方式)

   Action的上下文,该类提供了Action存在的环境. 也就是说通过该类能够获取到Action相关的一切数据.

   ActionContext

   getContext() 返回ActionContext实例对象

   get(key) 相当于 HttpServletRequest的getAttribute(String name)方法

   put(String,Object) 相当于HttpServletRequest的setAttribute方法

   getApplication() 返回一个Map对象,存取ServletContext属性

   getSession() 返回一个Map对象,存取HttpSession属性

   getParameters() 类似调用HttpServletRequest的getParameterMap()方法 

   setApplication(Map) 将该Map实例里key-value保存为ServletContext的属性名、属性值

   setSession(Map) 将该Map实例里key-value保持为HttpSession的属性名、属性值

   获取ActionContext对象:  ActionContext context = ActionContext.getContext();

简单的演示样例代码:

package cn.wwh.www.web.servletapi;

import java.lang.reflect.Array;
import java.util.Map; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 上午09:31:42
*/
public class ParamAction3 extends ActionSupport { private static final long serialVersionUID = 1L; public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Map<String,Object> paramMap = ctx.getParameters();
System.out.println(paramMap); //去paramMap.get("name")数组中索引为0的元素值
System.out.println(Array.get(paramMap.get("name"), 0)); //往request设置共享数据
ctx.put("name", "一叶扁舟");//request.setAttribute(key,Object)
Object requestValue = ctx.get("name");
System.out.println(requestValue); //往Session设置共享数据
//Map<String,Object> getSession()
Map<String,Object> sessionMap = ctx.getSession();
sessionMap.put("sessionKey", "sessionValue"); //往ServletContext中设置共享数据
//.Map<String,Object> getContextMap()
Map<String,Object> contextMap= ctx.getContextMap();
contextMap.put("appKey", "appValue");
return SUCCESS;
} }

注意在jsp中读取数据为:

        ${requestScope.name}<br />

        ${sessionScope.sessionKey}<br />

        ${appKey}





 5.通过ActionContext获取request、session、application解耦Map

(1) 对request域的操作

actionContext.put("name", "一叶扁舟") --> 相等与request.setAttribute("name", "一叶扁舟");

Object o = actionContext.get("name"); --> 等同与Object o = request.getAttribute("name");

(2).对session域的操作

Map<String,Object> sessionMap = ActionContext.getContext().getSession();

sessionMap.put("name", "一叶扁舟") --> 等同与session.setAttribute("name", "一叶扁舟");

Object o = sessionMap.get("name") --> 等同与Object o = session.getAttribute("name");

(3).对application域的操作

Map<String,Object> appMap = ActionContext.getContext().getApplication();

appMap.put("name", "一叶扁舟") --> 等同与servletContext.setAttribute("name", "一叶扁舟");

Object o = appMap.get("name") --> 等同与Object o = servletContext.getAttribute("name");

(4). 对请求參数的操作

Map<String,Object> paramMap = ActionContext.getContext().getParameters();

Object o = paramMap.get("username");

String[] values = (String[])o;

String username = values[0];

struts2訪问servlet的API的更多相关文章

  1. struts2中Action訪问servlet的两种方式

    一.IoC方式                在struts2框架中,能够通过IoC方式将servlet对象注入到Action中.通常须要Action实现下面接口: a. ServletRequest ...

  2. Struts07---访问servlet的API

    01.创建登录界面 <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  3. Python 訪问 LinkedIn (API)

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-16 @author: guaguastd @name: l ...

  4. Struts2(二)— Result结果配置、Servlet的API的访问、模型驱动、属性驱动

    一.Result结果配置 1.全局和局部结果 ​ 平常我们设置跳转页面,是在action标签里面加上 result标签来控制,这种设置的页面跳转,称之为局部结果页面但是我们有时候在很多个action里 ...

  5. struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素

    一.源码: struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE s ...

  6. struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素

    源码: strust.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  7. 在struts2中訪问servletAPI

    在struts2中訪问servletAPI,通俗点也就是使用servlet中的两个对象request对象和response对象. 前几天看到一个CRM项目的源代码,里面使用request对象和resp ...

  8. Struts2中使用Servlet API步骤

    Struts2中使用Servlet API步骤 Action类中声明request等对象 Map<String, Object> request; 获得ActionContext实例 Ac ...

  9. Struts2 Action与Servlet API耦合

    单元测试在开发中是非常重要的一个环节程序员在写完代码时,相应的单元测试也应写完整,否则你的代码就是不能让人信服的Struts2将Action与Servlet的API进行解耦之后,就使得单元测试变得非常 ...

随机推荐

  1. yyyy-MM-dd HH:mm:ss is Invalid Date in Safari, IE等浏览器下

    一.踩坑背景 在做某个项目的过程中,系统要求兼容safari,在使用Element-ui情况下,用到了datepicker组件,但是datepicker在type为daterange情况下,页面首次加 ...

  2. python学习笔记5.1-核心类型-集合set类型[转]

    转自:http://blog.csdn.net/business122/article/details/7541486 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系 ...

  3. HDU 4727 The Number Off of FFF (水题)

    The Number Off of FFF Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. Xilinx Platform Usb Cable

    Key Features High-performance FPGA configuration and PROM/CPLD programming Includes innovative FPGA- ...

  5. .NET的堆和栈03,引用类型对象拷贝以及内存分配

    在" .NET的堆和栈01,基本概念.值类型内存分配"中,了解了"堆"和"栈"的基本概念,以及值类型的内存分配.我们知道:当执行一个方法的时 ...

  6. RobotFramework自动化2-自定义关键字

    前言 有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.Selenium2library提供了Get Webelements 关键字,用于定位一组元素 以百 ...

  7. 使用开源库 MBProgressHUD 等待指示器

    source https://github.com/jdg/MBProgressHUD MBProgressHUD is an iOS drop-in class that displays a tr ...

  8. Android之使用XMLPull解析xml(二)

    转自:http://www.blogjava.net/sxyx2008/archive/2010/08/04/327885.html 介绍下在Android中极力推荐的xmlpull方式解析xml.x ...

  9. springboot 启动类CommandLineRunner(转载)

    在Spring boot项目的实际开发中,我们有时需要项目服务启动时加载一些数据或预先完成某些动作.为了解决这样的问题,Spring boot 为我们提供了一个方法:通过实现接口 CommandLin ...

  10. 使用spring中的@Transactional注解时,可能需要注意的地方

    前情提要 在编写业务层方法时,会遇到很多需要事务提交的操作,spring框架为我们提供很方便的做法,就是在需要事务提交的方法上添加@Transactional注解,比起我们自己开启事务.提交以及控制回 ...