struts之Action中获取request、response对象的方法

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 >

转载自: http://www.cnblogs.com/linjiqin/archive/2011/03/17/1987474.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值