码迷,mamicode.com
首页 > Windows程序 > 详细

struts2访问servlet的API

时间:2014-08-16 11:16:20      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:struts访问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,布布扣,bubuko.com

struts2访问servlet的API

标签:struts访问servlet的api

原文地址:http://blog.csdn.net/u011662320/article/details/38610093

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!