zoukankan      html  css  js  c++  java
  • struts之Action中获取request、response对象的方法

    1、访问或添加request/session/application属性

    public String scope() throws Exception{

        ActionContext ctx = ActionContext.getContext();

        ctx.getApplication().put("app", "应用范围");//往ServletContext里放入app

        ctx.getSession().put("ses", "session范围");//往session里放入ses

        ctx.put("req", "request范围");//往request里放入req

        return "scope";

    }

    JSP:

    <body>

        ${applicationScope.app} <br>

        ${sessionScope.ses}<br>

        ${requestScope.req}<br>

    </body>

                       

    2、获取HttpServletRequest/HttpSession/ServletContext/HttpServletResponse对象

    方法一、通过ServletActionContext类直接获取:

    public String rsa() throws Exception{

        HttpServletRequest request = ServletActionContext.getRequest();

        ServletContext servletContext = ServletActionContext.getServletContext();

        request.getSession();

        HttpServletResponse response = ServletActionContext.getResponse();

         return "scope";

    }

    方法二、实现指定接口,由struts框架运行时注入:

    public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{

        private HttpServletRequest request;

        private ServletContext servletContext;

        private HttpServletResponse response;

        public void setServletRequest(HttpServletRequest req) {

            this.request=req;

        }

        public void setServletResponse(HttpServletResponse res) {

            this.response=res;

        }

        public void setServletContext(ServletContext ser) {

            this.servletContext=ser;

        }

    }

               

    代码

    package com.ljq.action;

    import java.util.Arrays;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionContext;

    public class HelloWorldAction {

    //在页面中采用以下el表达式输出
    //第一种
    //${applicationScope.app}
    //${sessionScope.ses}
    //${requestScope.req}
    //第二种
    //${app}
    //${ses}
    //${req}
    public String list() {
    ActionContext ctx
    = ActionContext.getContext();
    ctx.getApplication().put(
    "app", "应用范围");// 往ServletContext里放入application
    ctx.getSession().put("ses", "session范围");// 往session里放入session
    ctx.put("req", "request范围");// 往request里放入request
    ctx.put("names", Arrays.asList("张三", "李四", "王五"));
    return "message";
    }

    //在页面中采用以下el表达式输出
    //第一种
    //${applicationScope.app}
    //${sessionScope.ses}
    //${requestScope.req}
    //第二种
    //${app}
    //${ses}
    //${req}
    public String get() throws Exception {
    HttpServletRequest request
    = ServletActionContext.getRequest();
    ServletContext servletContext
    = ServletActionContext.getServletContext();
    request.setAttribute(
    "req", "请求范围属性");
    request.getSession().setAttribute(
    "ses", "会话范围属性");
    servletContext.setAttribute(
    "app", "应用范围属性");
    // HttpServletResponse response = ServletActionContext.getResponse();
    return "message";
    }
    }




    jsp代码
    ${applicationScope.app}
    <br>
    ${sessionScope.ses}
    <br>
    ${requestScope.req}
    <br>
    ===========<br/>
    ${app}
    <br>
    ${ses}
    <br>
    ${req}
    <br>
    ===========<br/>
    <c:forEach items="${names}" var="name">
    ${name }
    <br/>
    </c:forEach>
  • 相关阅读:
    How to join (ap invoice distributions all) AP table info with PO Table
    Order Management APIs Oe_Order_Pub
    R12 Oe_Order_Pub API
    物料搬运单
    Process Order API In Order Management(详细)
    ap_invoice_distributions_all与PO表关联问题
    分配用人单位
    oe_order_pub 更改订单行数量,提示此更改的原因未提供或无效
    Order Management APIs
    提前期
  • 原文地址:https://www.cnblogs.com/linjiqin/p/1987474.html
Copyright © 2011-2022 走看看