首页 > 代码库 > action访问servlet的API并且获取到MAP类型的application,session,request

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;
    }
}

 

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