struts2訪问servlet的API
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的更多相关文章
- struts2中Action訪问servlet的两种方式
		
一.IoC方式 在struts2框架中,能够通过IoC方式将servlet对象注入到Action中.通常须要Action实现下面接口: a. ServletRequest ...
 - Struts07---访问servlet的API
		
01.创建登录界面 <%@ page language="java" import="java.util.*" pageEncoding="UT ...
 - Python 訪问 LinkedIn (API)
		
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-16 @author: guaguastd @name: l ...
 - Struts2(二)—  Result结果配置、Servlet的API的访问、模型驱动、属性驱动
		
一.Result结果配置 1.全局和局部结果  平常我们设置跳转页面,是在action标签里面加上 result标签来控制,这种设置的页面跳转,称之为局部结果页面但是我们有时候在很多个action里 ...
 - struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素
		
一.源码: struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE s ...
 - struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素
		
源码: strust.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
 - 在struts2中訪问servletAPI
		
在struts2中訪问servletAPI,通俗点也就是使用servlet中的两个对象request对象和response对象. 前几天看到一个CRM项目的源代码,里面使用request对象和resp ...
 - Struts2中使用Servlet API步骤
		
Struts2中使用Servlet API步骤 Action类中声明request等对象 Map<String, Object> request; 获得ActionContext实例 Ac ...
 - Struts2 Action与Servlet API耦合
		
单元测试在开发中是非常重要的一个环节程序员在写完代码时,相应的单元测试也应写完整,否则你的代码就是不能让人信服的Struts2将Action与Servlet的API进行解耦之后,就使得单元测试变得非常 ...
 
随机推荐
- chinese hacker-----WriteUp
			
原题地址:http://ctf5.shiyanbar.com/web/2/ 提示下载一个数据库 下载下来后发现是加密的 有密码,但发现密码不是4648 这里用到“DbView” 直接破解密码进入 发 ...
 - java设计模式(六)策略模式
			
适用于同一操作的不同行为,策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们可以相互替换,让算法独立于使用它的客户而独立变化,具体应用场景如第三方支付对接不同银行的算法. 要点:1)抽象策 ...
 - bzoj 3209 数位DP+欧拉定理
			
枚举1的个数,统计有那么多1的数的个数 /************************************************************** Problem: 3209 Us ...
 - Vue 生命周期方法
			
一.Vue 生命周期 Vue的生命周期即是实例从创建到销毁的一个过程.之前在学习Vue的时候,看过官网的教程,但是经常用到的是mounted,所以对其他生命周期方法不是很熟悉,这里有空做个总结,也方便 ...
 - 【JavaScript代码实现二】通用的事件侦听器函数
			
// event(事件)工具集,来源:github.com/markyun markyun.Event = { // 页面加载完成后 readyEvent : function(fn) { if (f ...
 - AES advanced encryption standard 2
			
/* * FIPS-197 compliant AES implementation * * Copyright (C) 2006-2007 Christophe Devine * * Redistr ...
 - Mybatis最入门---代码自动生成(generatorConfig.xml配置)
			
[一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...
 - VS 2010快捷键
			
1 注释选中的部分 Ctrl+K,C 2 取消注释的部分 Ctrl+K,U 3 设置断点 F9 取消此行的断点就再按一次F9 4 取消全部断点 ...
 - 基于nginx实现protobuf RPC
			
老婆一起来上海工作,每个月消费立马上来了,做了一个android记账应用,把每笔帐都实时记录进去.开始是单机版的,只能两个人分别记,月底再merge一下.刚好有一台阿里云的ECS,于是准备升级为带服务 ...
 - mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2
			
3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...