struts2方法怎样掉request
2个回答
展开全部
1, 如何在struts2里获得web资源
> request, response ,session , application
(1),调用struts2特有的API来取得web资源 ActionContext
Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
Map<String, Object> session = ActionContext.getContext().getSession();
Map<String, Object> application = ActionContext.getContext().getApplication();
request.put("name", "zhangfei");
session.put("age", 11);
application.put("sex", "man");
在jsp页面里从作用域取值
${requestScope.name }
${sessionScope.age }
${application.sex }
(2),控制器实现接口的方式来获得web资源 *Aware|| implements RequestAware,SessionAware,ApplicationAware
需要做三个全局变量 用set方法给全局变量赋值
@Override
public void setApplication(Map<String, Object> arg0) {
this.applicationMap = arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
this.sessionMap = arg0;
}
@Override
public void setRequest(Map<String, Object> arg0) {
this.requestMap = arg0;
}
(3),用API ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext application = ServletActionContext.getServletContext();
HttpSession session = request.getSession();
(4),通过实现接口的方式获得web资源 servlet*Aware || ServletRequestAware,ServletResponseAware,ServletContextAware
@Override
public void setServletContext(ServletContext arg0) {
this.application = arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
> request, response ,session , application
(1),调用struts2特有的API来取得web资源 ActionContext
Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
Map<String, Object> session = ActionContext.getContext().getSession();
Map<String, Object> application = ActionContext.getContext().getApplication();
request.put("name", "zhangfei");
session.put("age", 11);
application.put("sex", "man");
在jsp页面里从作用域取值
${requestScope.name }
${sessionScope.age }
${application.sex }
(2),控制器实现接口的方式来获得web资源 *Aware|| implements RequestAware,SessionAware,ApplicationAware
需要做三个全局变量 用set方法给全局变量赋值
@Override
public void setApplication(Map<String, Object> arg0) {
this.applicationMap = arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
this.sessionMap = arg0;
}
@Override
public void setRequest(Map<String, Object> arg0) {
this.requestMap = arg0;
}
(3),用API ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext application = ServletActionContext.getServletContext();
HttpSession session = request.getSession();
(4),通过实现接口的方式获得web资源 servlet*Aware || ServletRequestAware,ServletResponseAware,ServletContextAware
@Override
public void setServletContext(ServletContext arg0) {
this.application = arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-03-28 · 知道合伙人数码行家
关注
展开全部
获得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;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询