zoukankan      html  css  js  c++  java
  • Struts2中获取request,session,application对象

    第一种方式:获取map类型的request,session,application

    package com.maya.action;
    
    import java.util.Map;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class RSAAction extends ActionSupport {
        
        //获取map类型的request,session,application
        private Map<String, Object> request;
        private Map<String, Object> session;
        private Map<String, Object> application;
        private ActionContext context;
        //为了方便在构造函数中获取ActionContext对象
        public RSAAction(){
            context=ActionContext.getContext();
        }
        //以下获取map类型的request,session,application
        @Override
        public String execute() throws Exception {
            //获取map类型的request
            request=(Map<String, Object>) context.get("reqeust");
            request.put("key", "value");
            
            //获取map类型的session
            session=context.getSession();
            session.put("key", "value");
            
            //获取map类型的application
            application=context.getApplication();
            application.put("key", "value");
                    
            return super.execute();
        }    
    }

    第二种方式:采用接口方式获得map类型的request、session、application

    package com.maya.action;
    
    import java.util.Map;
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.RequestAware;
    import org.apache.struts2.interceptor.SessionAware;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class RSAAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
        private Map<String, Object> request;
        private Map<String, Object> session;
        private Map<String, Object> application;
        
        //获取reqeust
        @Override  
        public void setRequest(Map<String, Object> reqeust) {
            this.request=reqeust;
            
        }
        //获取session
        @Override
        public void setSession(Map<String, Object> session) {
            this.session=session;
            
        }
        //获取application
        @Override
        public void setApplication(Map<String, Object> application) {
            this.application=application;
            
        }
        
        @Override
        public String execute() throws Exception {
            //赋值方式
            request.put("key", "value");
            session.put("key", "value");
            application.put("key", "value");        
            return super.execute();
        }    
    }

    第三种方式:获取Http类型的request、session、application

    package com.maya.action;
    
    import java.util.Map;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class RSAAction extends ActionSupport {
        //获取Http类型的request、session、application
        private HttpServletRequest request;
        private HttpSession session;
        private ServletContext application;
        
        public RSAAction(){
            request=ServletActionContext.getRequest();
            session=request.getSession();
            application= session.getServletContext();
        }
        
        @Override
        public String execute() throws Exception {
            
            request.setAttribute("key", "value");
            session.setAttribute("key", "value");
            application.setAttribute("key", "value");        
            
            return super.execute();
        }    
    }
  • 相关阅读:
    cocos2dx[3.2](2) 3.x巨变
    cocos2dx[3.2](1) 浅析cocos2dx3.2引擎目录
    cocos2dx基础篇(28) 布景层Layer的三个子类
    cocos2dx基础篇(27) 屏幕适配
    centos 安装 mysql5.6
    centos 安装 mysql5.7.9初始密码问题
    php 计算字符串长度
    mysql tinyint
    php数组
    PHP中9大缓存技术总结
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6547555.html
Copyright © 2011-2022 走看看