开始学习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")>

你可能感兴趣的:(struts)