在struts2的Action中,操作域对象一共有三种方式:
1.ActionContext(与servelt API无关联):
//相当于request
ActionContext.getContext().put("userName", user.getUserName());
//相当于session
ActionContext.getContext().getSession().put("userName", user.getUserName());
//相当于Application
ActionContext.getContext().getApplication().put("userName", user.getUserName());
//parameter
String name = (String) ActionContext.getContext().getParameters().get("userName");
2.实现ServletRequestAware,ServletResponseAware接口,通过注入的方式,获取request、response:
private HttpServletResponse response; private HttpServletRequest request; @Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
} @Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
3.通过ServletActionContext提供的静态方法获得request、response:
HttpServletRequest request = ServletActionContext.getRequest();