Struts2:訪問Application,Session和Request范圍內的值[筆記]
The framework provides several access helpers to access Session, Application, Request scopes.
框架提供了多種訪問Session,Application和Request范圍內值的幫助器。
Web agnostic (independent of the servlet API) with a single line of code.
Web agnostic (獨立于Servlet API)用一個簡單的代碼。
通過context entry
Map session = (Map) ActionContext.getContext().get("session");
session.put("myId",myProp);
通過幫助器方法
ServletActionContext.getRequest().getSession()
![]() |
Do not use ActionContext.getContext() in the constructor of your Action class. The values may not be set up, and the call may return null for getSession(). 別在Action的構造函數里用ActionContext.getContext(),那個值可能沒有建立,可能返回null。 |
If you must have get access to the HttpSession, use the ServletConfigInterceptor (see test:Interceptors).
如果你必須訪問HttpSession,用ServletConfigInterceptor。
In your views, you can access with your JavaServer Pages with calls to the implicit properties session and request.
下面展示了在jsp中如果和訪問session和request
<saf: property value="#session.myId" />
<saf: property value="#request.myId" />
All the servlet scopes can be accessed via the ActionContext.
下面展示了在servlet范圍如何通過ActonContext訪問:
Map request = (Map) ActionContext.getContext().get("request");
request.put("myId",myProp);
Map application = (Map) ActionContext.getContext().get("application");
application.put("myId",myProp);
Map session = (Map) ActionContext.getContext().get("session");
session.put("myId", myProp);
Map attr = (Map) ActionContext.getContext().get("attr");
attr.put("myId",myProp);
The attr map will search the javax.servlet.jsp.PageContext for the specified key. If the PageContext doean't exist, it will search request}, {{session, and application respectively.
posted on 2007-04-29 09:48 MingIsMe 閱讀(2203) 評論(0) 編輯 收藏 所屬分類: Struts2學習