zoukankan      html  css  js  c++  java
  • struts2访问或添加几个属性(request/session/application属性)

    https://blog.csdn.net/hebiao100/article/details/7385055

    struts2添加request、session、application属性

    第一种方法:

    ActionContext ctx = ActionContext.getContext();  
        ctx.put("r1", list);//向request中添加属性  
        ctx.getSession().put("s1",list);//向session中添加属性  
        ctx.getApplication().put("a1", list);//向application中添加属性  
    ctx.put("names", Arrays.asList("小航,小王,小韩"));//向request中添加属性
     
    HttpSession session =null;  
            session = ServletActionContext.getRequest().getSession();  
            if(session.getAttribute("user") != null) {  
                session.removeAttribute("user");  
            }  
            session.setAttribute("user", user);  
    Map<String,Object> session = null;  
            session = ActionContext.getContext().getSession();  
            if(session.containsKey("user")){  
                session.remove("user");  
            }  
              
            session.put("user", user); 

    第二种方法:

    使用 ServletRequestAware接口

    public class LoginAction4 extends ActionSupport implements ServletRequestAware {  
          
        private HttpServletRequest request;  
        private HttpSession session;  
        private ServletContext application;  
          
          
          
        public String execute() {  
            request.setAttribute("r1", "r1");  
            session.setAttribute("s1", "s1");  
            application.setAttribute("a1", "a1");  
            return SUCCESS;   
        }  
      
      
      
        @Override  
        public void setServletRequest(HttpServletRequest request) {  
            this.request = request;  
            this.session = request.getSession();  
            this.application = session.getServletContext();  
        }  
          
    }  

    struts2访问request、session、application属性

    第一种方法:

    使用struts tag:

    <s:property value="#request.r1"/>   
    <s:property value="#session.s1"/>   
    <s:property value="#application.a1"/>   

    第二种方法:

    使用jsp对象

    <%=request.getAttribute("r1") %> <br />  
    <%=session.getAttribute("s1") %> <br />  
    <%=application.getAttribute("a1")%> <br />  
    <c:forEach items="${names}" var="name">
       ${name}
    
    </c:forEach>
     
  • 相关阅读:
    Thumbnailator压缩图片
    dubbo序列化的一点注意
    Java编程思想读书笔记之内部类
    Hello World
    sql中where和having的区别
    Linux下服务器搭建
    maven中profile的激活方式
    <![CDATA[ ]]>
    linux下用xampp安装php集成环境,并修改各自端口号
    关于星号(**/*.java)
  • 原文地址:https://www.cnblogs.com/feifeicui/p/8733152.html
Copyright © 2011-2022 走看看