0

Based from what I've researched, I've seen that tags such as <s:set>, <s:push> or by creating an <s:bean> are able to insert references directly to the ActionContext or ValueStack. This confuses me a lot because why can't you just have one dedicated place to store everything? Probably just put everything in the ActionContext since it's basically acts as a ServletContext.

To make it even more confusing, if you wanted to access values in the ValueStack, you'll have to use Struts tags such as <s:property> but if the value's just stored in the ActionContext, you just use the #value prefix provided by OGNL.

Can someone please clear this up for me? When I used Spring, I believe everything that I needed (request, session, applicationContext) was inside the ServletContext and to access these values on my webpage, I could just use the $ prefix to access anything within context.

Roman C
  • 49,761
  • 33
  • 66
  • 176
mpmp
  • 2,409
  • 4
  • 33
  • 46
  • 1. *one dedicated place to store everything* - One of the things to think about is `scope`. Why to declare local variables in method? 2. *use the #value prefix* - Not clear what do you mean. 3. You can still use `$` with S2. 4. Just use what you need to and if something doesn't work then ask a specific question. – Aleksandr M Feb 27 '15 at 09:53
  • Struts1, Spring MVC, etc. are all Push-MVC frameworks. Struts2 is Pull-MVC @Miguel. You don't push values in the request, you put them in the ValueStack, and then pull'em out from the JSP with OGNL, JSTL or whatever. Actually, ValueStack is a smart thing, when compared to the other frameworks. – Andrea Ligios Feb 27 '15 at 14:34
  • You can use `$` to access (most) everything in S2, too. The reason "why" is because that was how the WebWork developers decided to do it. And no, you aren't *required* to use the S2 tags to access stack values. There are two scopes in the value stack: the stack itself, and named items in the context. That's just the way it is. The stack is nice for layering stuff. – Dave Newton Feb 27 '15 at 16:34
  • So generally, what's the best practice to pull values from the ActionContext/ValueStack to be displayed in the JSP? The use of `#`? or Struts tags? or just the generic `$`? @DaveNewton – mpmp Feb 27 '15 at 18:34
  • @MiguelPortugal IMO it just depends. For simple stuff where I don't really need to mess with OGNL I'll often just use JSP EL. I don't use `#` vars very much. – Dave Newton Feb 27 '15 at 18:50

1 Answers1

0

Every place has its dedicated storage where you can put your objects for later use/retrieve running across some invocation context. Whatever context is running the framework is associated. The context is the way of communicating between scoped objects inside it you can access using Java or other expression language (EL) like OGNL.

In OGNL the action context is the OGNL context, and the value stack is a root.

The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.) Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext, alongside the value stack (our OGNL root).

The ActionContext is ThreadLocal, so you can use it in one thread. The best way to get the action context/value stack from this thread is using static method.

ActionContxt ctx = ActionContext.getContext();
ValueStack vs = ctx.getValueStack();

Interceptors also have a parameter passed, known as invocation context, which is the action context.

The value stack has also its own context, the validation has its own context. So, these definitions never end.

Roman C
  • 49,761
  • 33
  • 66
  • 176