struts的Action中,有三种方法可以得到request、session、servletContext
原创    cannot    发表于:2017-08-07 20:41:12
  阅读 :74   收藏   编辑
1.通过ServletActionContext类
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext servletContext = ServletActionContext.getServletContext();
2.通过ActionContext类获取Map
ActionContext context = ActionContext.getContext();
Map<String, Object> request = context.getContextMap();
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication()
3.通过实现接口,注入map对象
public class DataAction extends ActionSupport implements RequestAware, SessionAware,ApplicationAware{
    Map<String,Object> request;
    Map<String,Object> session;
    Map<String,Object> application;

    public String execute()throws Exception{
        request.put("request_data","request_data_注入");
        session.put("session_data","session_data注入");
        application.put("servletcontext_data","servletcontext_data_注入");
        return SUCCESS;
    }

    @Override
    public void setRequest(Map<String, Object> map) {
        this.request = map;
    }

    @Override
    public void setApplication(Map<String, Object> map) {
        this.application = map;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }
}

=

评论
条评论