zoukankan      html  css  js  c++  java
  • 关于ActionContext.getContext()的用法心得

    为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext对应的Map对象来保存和读取数据。

    servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是通过 getServletContext()方法获得的,由于HttpServlet继承Servlet的关系GenericServlet类和HttpServlet类同时具有该方法。 

    SevletActionContext是与外面容器耦合的,但可以得到HttpSeveletSession
    通过ServletActionContext.getRequest 得到当前HttpServletRequest 对象的
    引用,从而直接与Web 容器交互。

    (一)通过ActionContext来获取request、session和application对象的LoginAction1

    [java] view plain copy
     
    1. ActionContext context = ActionContext.getContext();   
    2. Map request = (Map)context.get("request");  
    3. Map session = context.getSession();  
    4. Map application = context.getApplication();  
    5. request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。  
    6. session.put("user", user);//在session中保存user对象  
    7. application.put("counter", count);  

      

    在JSP中读取

    [xhtml] view plain copy
     
    1. <body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>  
    2. </body>  

    (二)直接使用ActionContex类的put()方法

    ActionContext.getContext().put("greeting", "欢迎您来到http://www. sunxin.org");

    然后在结果页面中,从请求对象中取出greeting属性,如下:

    ${requestScope.greeting} 或者 <%=request.getAttribute("greeting")%>

  • 相关阅读:
    Qt调用外部程序QProcess通信
    QT错误:collect2:ld returned 1 exit status
    ARM编译空间属性(转)
    深入C语言内存区域分配(进程的各个段)详解(转)
    Linux系统的组成和内核的组成
    C语言中,头文件和源文件的关系(转)
    Ubuntu安装samba服务器
    2018年应该做的事
    生活经历1
    学习笔记
  • 原文地址:https://www.cnblogs.com/bzggood/p/6519986.html
Copyright © 2011-2022 走看看