首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Struts2透过使用ActionContext类获取request和response对象

2012-11-06 
Struts2通过使用ActionContext类获取request和response对象使用ActionContext类这种方法比较简单,我们可以

Struts2通过使用ActionContext类获取request和response对象

使用ActionContext类
这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:

HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);HttpServletRequest  request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

?

?

使用ServletActionContext类
Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。
这就是org.apache.struts2.ServletActionContext类。
我们可以直接使用ServletActionContext类的getRequest、getResponse方法来获得
HttpServletRequest、HttpServletResponse对象。

?

代码如下:

  HttpServletResponse response = ServletActionContext.getResponse();  response.getWriter().write("hello world");

?

热点排行