struts2怎么获取request
获得request对象有三种方式:
方式一:aware接口注入,即实现ServletRequestAware接口(此方式最为麻烦)
public class Demo2ServletAction extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
@Override
public String execute() throws Exception {
request.setAttribute("gf", "request_aware");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
方式二:使用ServletActionContext工具类(此方式虽好,但与servlet api耦合)
public class Demo1ServletAction extends ActionSupport{
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setAttribute("gf", "request");
return SUCCESS;
}
}
方式三:通过ActionContext来获得(此方式与servlet api解耦,建议使用)
public class Demo3ServletAction extends ActionSupport{
@Override
public String execute() throws Exception {
ActionContext.getContext().put("gf", "request");
return SUCCESS;
}
}
获得request对象:
A.HttpServletRequest request = ServletActionContext.getRequest ();
B.ActionContext ct= ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ct.get(
ServletActionContext.HTTP_REQUEST);