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();
        }    
    }
  • 相关阅读:
    在 Mac OS X 上安装 TensorFlow
    用序列到序列和注意模型实现的:Translation with a Sequence to Sequence Network and Attention
    PyTorch 实战-张量
    PyTorch 实战-用 Numpy 热身
    tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
    叩响秋雨梧桐的大门——2018中考之后
    完全背包——01背包方法数
    dp——01背包
    图论最短路——dijkstra
    图论最短路——spfa
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6547555.html
Copyright © 2011-2022 走看看