开始学习Struts【 访问request,session, application 对象】

ActionContext 是action执行的上下文,struts2对 HttpServletRequest,HttpSession,ServletContext进行了封装,构造三个Map对象来代替这三个对象

通过ActionContext来获取 request,session ,application 对象实例

ActionContext context=ActionContext.getContext();
Map request=(Map)context.get("request");
Map session =(Map)context.getSession();
Map application=(Map)context.getApplication();

//在请求中放置欢迎信息
request.put("welcome","欢迎您的到来");

//在session中放置User对象
User user =new User();
user.setUserName(userName);
session.put("user", user);

//统计用户的访问量,在Application中保存用户的访问量
Object o=application.get("counter");
if(null==o)
{
o=Integer.valueOf(0);
}
else
{
int count=Integer.parseInt(o.toString());
count++;
application.put("counter",String.valueOf(count));

}
return SUCCESS;




利用请求对象传递数据还有一种方式:
ActionContext.getContext().put("welcome","欢迎您的到来");

然后在结果页面中重请求对象取出welcome的值
${requestScope.welcome}或<%request.getAttribute("welcome")>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值