首页 > 代码库 > Struts2 -- 获取Servlet API

Struts2 -- 获取Servlet API

在struts2中获取Servlet API有三种方式:

1.通过ActionContext来获取

        1.获取一个ActionContext对象。
                ActionContext context=ActionContext.getContext();
            2.获取servlet api
                注意:通过ActionContext获取的不是真正的Servlet API,而是一个Map集合。
                
                1.context.getApplication()
                2.context.getSession()
                3.context.getParameter();---得到的就相当于request.getParameterMap()
                4.context.put(String,Object) 相当于request.setAttribute(String,String);

    

public class Demo01Action extends ActionSupport{    @Override    public String execute() throws Exception {                ActionContext context = ActionContext.getContext();        Map<String, Object> application = context.getApplication();        Map<String, Object> session = context.getSession();        Map<String, Object> parameters = context.getParameters();        context.put(key, value);                return null;    }}

 

 

2.注入方式获取(这种方式是真正的获取到了Servlet API)

    1.要求action类必须实现提定接口。
                ServletContextAware : 注入ServletContext对象
                ServletRequestAware :注入 request对象
                ServletResponseAware : 注入response对象

            2.重定接口中的方法。                
                private HttpServletRequest request;


            3.声明一个web对象,使用接口中的方法的参数对声明的web对象赋值.    
                public void setServletRequest(HttpServletRequest request) {
                    this.request = request;
                } 

public class Demo01Action extends ActionSupport implements ServletContextAware,        ServletRequestAware, ServletResponseAware {    private ServletContext context;    private HttpServletRequest request;    private HttpServletResponse response;    @Override    public String execute() throws Exception {        return null;    }    @Override    public void setServletContext(ServletContext context) {        this.context = context;    }    @Override    public void setServletRequest(HttpServletRequest request) {        this.request = request;    }    @Override    public void setServletResponse(HttpServletResponse response) {        this.response = response;    }}

扩展:分析其实现:
        是使用struts2中的一个interceptor完成的.
            <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
            源码:

public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {    private static final long serialVersionUID = 605261777858676638L;    /**     * Sets action properties based on the interfaces an action implements. Things like application properties,     * parameters, session attributes, etc are set based on the implementing interface.     *     * @param invocation an encapsulation of the action execution state.     * @throws Exception if an error occurs when setting action properties.     */    public String intercept(ActionInvocation invocation) throws Exception {        final Object action = invocation.getAction();        final ActionContext context = invocation.getInvocationContext();     //判断当前的action是否继承指定方法        if (action instanceof ServletRequestAware) {     
       //获取reqeust对象,并将request对象封装到action中 HttpServletRequest request
= (HttpServletRequest) context.get(HTTP_REQUEST); ((ServletRequestAware) action).setServletRequest(request); } if (action instanceof ServletResponseAware) { HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE); ((ServletResponseAware) action).setServletResponse(response); } if (action instanceof ParameterAware) { ((ParameterAware) action).setParameters((Map)context.getParameters()); } if (action instanceof ApplicationAware) { ((ApplicationAware) action).setApplication(context.getApplication()); } if (action instanceof SessionAware) { ((SessionAware) action).setSession(context.getSession()); } if (action instanceof RequestAware) { ((RequestAware) action).setRequest((Map) context.get("request")); } if (action instanceof PrincipalAware) { HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); if(request != null) { // We are in servtlet environment, so principal information resides in HttpServletRequest ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request)); } } if (action instanceof ServletContextAware) { ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT); ((ServletContextAware) action).setServletContext(servletContext); } return invocation.invoke(); }}

 

3.通过ServletActionContext获取.
            在ServletActionContext中方法都是static。            
            getRequest();
            getResposne();
            getPageContext();

  

public class Demo01Action extends ActionSupport {    @Override    public String execute() throws Exception {                HttpServletRequest request = ServletActionContext.getRequest();                HttpServletResponse response = ServletActionContext.getResponse();                PageContext pageContext = ServletActionContext.getPageContext();                ServletContext servletContext = ServletActionContext.getServletContext();        return null;    }}

 

 

 

技术分享

 

 


               

 

Struts2 -- 获取Servlet API