Struts2与Servlet API完全分离。低耦合
但Action经常需要访问Servlet中的对象:request、session、application。。。
于是提供了ActionContext类,可以通过该类获得Servlet API。
ActionContext是Action的上下文对象,Action运行期间所用到的数据都保存在ActionContext里。例如:session
//将登录信息设置到session中(借助session演示重定向问题)
ActionContext.getContext().getSession().put("user", username);
//设置到request中(采用如下方式访问request对象 )
ActionContext.getContext().put("user", username);
//想访问Application(采用如下方式访问Application对象 )
ActionContext.getContext().getApplication().put("user", username);
//使用request.getParameter取得表单数据
ActionContext.getContext().getParameters().get("username");