• struts如何在Action类中操作request,session


      在servlet中,通过request.getparameter与setparameter来实现后端与前端jsp页面的数据交互,那么在struts中,也有几种方式来操作request,session实现后端与前端的交互。

    方法一、利用ActionContext.getContext().get("request");

    Map<String,Object> request = (Map<String,Object>)ActionContext.getContext().get("request");
    request.put(key, value);
    request.get("key");
    
    Map<String,Object> session = ActionContext.getContext().getSession();
    session.put(key, value);
    session.get(key);

    方法二、使用ServletActionContext.getRequest();

    HttpServletRequest request = ServletActionContext.getRequest();
    request.setAttribute(arg0, arg1);
    request.getAttribute(arg0)
    
    HttpServletRequest req = ServletActionContext.getRequest();
    HttpSession session = req.getSession();
    session.setAttribute(arg0, arg1);
    session.getAttribute(arg0);

    方法三、使用RequestAware,SessionAware接口

    1.实现这两个接口

    public class NewsAction extends ActionSupport implements RequestAware,SessionAware {}

    2.定义两个成员变量

       private Map<String,Object> session;
        private Map<String,Object> request;

    3.重写接口中的方法

        @Override
        public void setRequest(Map<String, Object> arg0) {
            this.request = arg0;
            
        }
        @Override
        public void setSession(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            this.session = arg0;
        }    

    4.使用session.put(),session.get(),request.put(),request.get()方法实现

    public class NewsAction extends ActionSupport implements RequestAware,SessionAware {
        private Map<String,Object> session;
        private Map<String,Object> request;
      @Override
        public void setRequest(Map<String, Object> arg0) {
            this.request = arg0;
            
        }
        @Override
        public void setSession(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            this.session = arg0;
        }
    }
  • 相关阅读:
    Spring Boot中实现logback多环境日志配置
    阿里云ECSLinux系统下挂载磁盘(转)
    解决 No qualifying bean of type 问题
    通过rpm离线安装Oracle 19C
    ADFS配置踩坑记
    .NET Core 2.0下载和文档
    .NET Core 2.0和ASP.NET Core 2.0正式版抢先体验
    .NET 微服务和Docker容器
    DocFX生成PDF文档
    ASP.NET Core 开源论坛项目 NETCoreBBS
  • 原文地址:https://www.cnblogs.com/claricre/p/6548874.html
Copyright © 2020-2023  润新知