action访问servlet的API并且获取到MAP类型的application,session,request

Posted 言凡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了action访问servlet的API并且获取到MAP类型的application,session,request相关的知识,希望对你有一定的参考价值。


public
class testAction3 extends ActionSupport { private Map<String,Object> request; private Map<String,Object> session; private Map<String,Object> application; /** * 构造函数,没有返回值 */ public testAction3(){ this.request=(Map) ActionContext.getContext().get("request"); this.session=ActionContext.getContext().getSession(); this.application=ActionContext.getContext().getApplication(); } @Override public String execute(){ System.out.println("开始。。。。"); this.request.put("reqAttr","reqDirect"); this.session.put("sessionAttr", "sessionDirect"); this.application.put("appAttr", "appDirect"); return SUCCESS; } }
 <body>
    request:${reqAttr}<br>
    session:${sessionAttr}<br>
    application:${appAttr}<br>
    ------------------------------<br>
    request:${requestScope.reqAttr}<br>
    session:${sessionScope.sessionAttr}<br>
    application:${applicationScope.appAttr}<br>
  </body>

或者使用IoC方法:需要实现相应的接口

public class testAction3 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
    
    private Map<String,Object> request;
    private Map<String,Object> session;
    private Map<String,Object> application;
    
    @Override
    public void setApplication(Map<String, Object> application) {
        // TODO Auto-generated method stub
        this.application=application;
    }
    @Override
    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub
        this.session=session;
    }
    @Override
    public void setRequest(Map<String, Object> request) {
        // TODO Auto-generated method stub
        this.request=request;
    }

    @Override
    public String execute(){
        System.out.println("开始。。。。");
        this.request.put("reqAttr","reqDirect");
        this.session.put("sessionAttr", "sessionDirect");
        this.application.put("appAttr", "appDirect");
        return SUCCESS;
    }
}

 二、获取http类型

 

public class testAction3 extends ActionSupport{
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    private ServletContext application;
    /**
     * 获取
     */
    public testAction3(){
        this.request=ServletActionContext.getRequest();
        this.response=ServletActionContext.getResponse();
        this.session=this.request.getSession();
        this.application=ServletActionContext.getServletContext();
    }

    @Override
    public String execute() throws Exception{
        System.out.println("开始。。。。");
        this.request.setAttribute("reqAttr", "reqDirect");
        this.application.setAttribute("appAttr", "applicationDirect");
        this.session.setAttribute("sessionAttr", "sessionDirect");
//        Ajax使用的时候可以用    
//        PrintWriter out=response.getWriter();
//        out.print("response");
//        out.flush();
//        out.close();
//        return null;
        return SUCCESS;
    }
}

 

或者用依赖注入方式:

public class testAction3 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    private ServletContext application;
    /**
     * 获取
     */
    @Override
    public void setServletContext(ServletContext application) {
        // TODO Auto-generated method stub
        this.application=application;
    }
    @Override
    public void setServletResponse(HttpServletResponse response) {
        // TODO Auto-generated method stub
        this.response=response;
    }
    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;
    }

    @Override
    public String execute() throws Exception{
        this.session=this.request.getSession();//session是通过request获取,所以没有关于实现session的接口
        System.out.println("开始。。。。");
        this.request.setAttribute("reqAttr", "reqDirect");
        this.application.setAttribute("appAttr", "applicationDirect");
        this.session.setAttribute("sessionAttr", "sessionDirect");
        return SUCCESS;
    }


}

 

以上是关于action访问servlet的API并且获取到MAP类型的application,session,request的主要内容,如果未能解决你的问题,请参考以下文章

5.struts2中Action类中获取ServletAPI的三种方式

Action访问Servlet API

Action访问Servlet API的三种方法

Struts2笔记--Action访问Servlet API

在Struts2框架中使用Servlet的API

struts2 servlet api 访问方式