This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch feature/accessing-scopes in repository https://gitbox.apache.org/repos/asf/struts-site.git
commit 808ae69c19b10ab5373104d1a928e60943c25a52 Author: Lukasz Lenart <[email protected]> AuthorDate: Wed Aug 16 14:22:32 2023 +0300 Updates how to access scopes --- ...ccessing-application-session-request-objects.md | 51 ++++++++++++---------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/source/core-developers/accessing-application-session-request-objects.md b/source/core-developers/accessing-application-session-request-objects.md index 0988e02d8..0cd2ca8ee 100644 --- a/source/core-developers/accessing-application-session-request-objects.md +++ b/source/core-developers/accessing-application-session-request-objects.md @@ -8,44 +8,49 @@ parent: # Accessing application, session, request objects -**DEPRECATED???** - The framework provides several access helpers to access Session, Application, Request scopes. ## Accessing from Java -All the JEE scope attribute maps can be accessed via `ActionContext`. +The best way to access Request, Session or Application scope is to use one of the following interfaces: +- `ServletRequestAware` - to access Request scope +- `ServletResponseAware` - to access Response scope +- `SessionAware` - to access Session scope +- `ApplicationAware` - to access Application scope -**Accessing servlet scopes** +Example usage of the interfaces: ```java -Map attr = (Map) ActionContext.getContext().get("attr"); -attr.put("myId", myProp); // Page scope. - -Map application = (Map) ActionContext.getContext().get("application"); -application.put("myId", myProp); - -Map session = (Map) ActionContext.getContext().get("session"); -session.put("myId", myProp); - -Map request = (Map) ActionContext.getContext().get("request"); -request.put("myId", myProp); +public class MyAction implements ApplicationAware { + + private Map<String, Object> application; + + public void withApplication(Map<String, Object> application) { + this.application = application; + } + + public String execute() { + application.set("myKey", "myValue"); + ... + return "success"; + } + +} ``` -> 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(). +Implementing `ServletRequestAware` or `ServletResponseAware` will tie your actions to Servlet objects. Yet using these +interfaces and `SessionAware` or `ApplicationAware` combined with the `servletConfig` interceptor, is the best way +to access these scopes. -We can also access the `HttpServletRequest` and `HttpServletResponse` objects themselves through `ServletActionContext`. -In general this isn't recommended as it will tie our action to the servlet specification. +### Avoid using ActionContext -**Setting session attribute through session object** +Using `ActionContext` directly is a bad practice and should be avoided, instead of using ```java -ServletActionContext.getRequest().getSession().put("myId", myProp); +ActionContext.getContext().getSession().put("myAttribute", "myValue"); ``` -Implementing `ServletRequestAware` or `ServletResponseAware`, combined with the `servletConfig` interceptor, -is an alternative way to access the request and response objects, with the same caveat. +use one of the `*Aware` interfaces above. ## Accessing from the view (JSP, FreeMarker, etc.)
