You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2023/08/16 11:30:58 UTC
[struts-site] branch feature/accessing-scopes updated (808ae69c1 -> ae2e769a0)
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a change to branch feature/accessing-scopes
in repository https://gitbox.apache.org/repos/asf/struts-site.git
discard 808ae69c1 Updates how to access scopes
new ae2e769a0 Updates how to access scopes
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (808ae69c1)
\
N -- N -- N refs/heads/feature/accessing-scopes (ae2e769a0)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../core-developers/accessing-application-session-request-objects.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[struts-site] 01/01: Updates how to access scopes
Posted by lu...@apache.org.
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 ae2e769a0537a563437e858a84fb4bd52194be88
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Wed Aug 16 14:22:32 2023 +0300
Updates how to access scopes
---
...ccessing-application-session-request-objects.md | 54 ++++++++++++----------
1 file changed, 30 insertions(+), 24 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..7a596fe8c 100644
--- a/source/core-developers/accessing-application-session-request-objects.md
+++ b/source/core-developers/accessing-application-session-request-objects.md
@@ -8,44 +8,50 @@ parent:
# Accessing application, session, request objects
-**DEPRECATED???**
-
-The framework provides several access helpers to access Session, Application, Request scopes.
+The framework provides several access helpers to access Request, Session, Application scopes.
+See [Servlet Config Interceptor](servlet-config-interceptor) page to find all the supported interfaces.
## 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.)