Struts2_05_访问Servlet API

a.Map 类型 servlet 对象

通过特定的API

 

public class AttrAction {

 

    private Map<String,Object>  requestAttrs;

    private Map<String,Object>  sessionAttrs;

    private Map<String,Object>  applicationAttrs;

 

    public AttrAction() {

       requestAttrs=(Map<String, Object>) ActionContext.getContext().get("request");

       sessionAttrs=ActionContext.getContext().getSession();

       applicationAttrs=ActionContext.getContext().getApplication();

      

    }

}

 

依赖注入

public class Attr2Action implements RequestAware, SessionAware,

       ApplicationAware {

 

    private Map<String, Object> requestAttrs;

    private Map<String, Object> sessionAttrs;

    private Map<String, Object> applicationAttrs;

 

    public void setRequest(Map<String, Object> requestAttrs) {

       this.requestAttrs = requestAttrs;

    }

 

    public void setSession(Map<String, Object> sessionAttrs) {

       this.sessionAttrs = sessionAttrs;

    }

 

    public void setApplication(Map<String, Object> applicationAttrs) {

       this.applicationAttrs = applicationAttrs;

    }

 

}     

 

b.拿真实的servlet API对象

通过特定的API

public class ServletAction {

 

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HttpSession session;

    private ServletContext context;

 

    public ServletAction() {

       request = ServletActionContext.getRequest();

       response = ServletActionContext.getResponse();

       session = request.getSession();

       context = ServletActionContext.getServletContext();

    }

}

 

依赖注入

public class Servlet2Action implements ServletRequestAware,

       ServletResponseAware, ServletContextAware {

 

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HttpSession session;

    private ServletContext context;

 

    public void setServletRequest(HttpServletRequest request) {

       this.request = request;

       this.session = request.getSession();

    }

 

    public void setServletResponse(HttpServletResponse response) {

       this.response = response;

    }

 

    public void setServletContext(ServletContext servletContext) {

       this.context = servletContext;

    }

}

 

你可能感兴趣的:(struts2)