在struts2的action里面怎么获得request
2个回答
2016-07-05
展开全部
获得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;
}
}
方式一: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;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询