关于ActionContext.getContext()的用法心得
(2011-05-12 11:21:48) 标签: actioncontextgetcontext()it | 分类: Struts |
(一)通过ActionContext来获取request、session和application对象的LoginAction1
view plaincopy to clipboardprint?
ActionContext context = ActionContext.getContext();
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。
session.put("user", user);//在session中保存user对象
application.put("counter", count);
ActionContext context = ActionContext.getContext();
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。
session.put("user", user);//在session中保存user对象
application.put("counter", count);
在JSP中读取
view plaincopy to clipboardprint?
<body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>
</body>
<body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>
</body>
(二)直接使用ActionContex类的put()方法
ActionContext.getContext().put("greeting", "欢迎您来到http://www. sunxin.org");
然后在结果页面中,从请求对象中取出greeting属性,如下:
${requestScope.greeting} 或者 <%=request.getAttribute("greeting")%>