Search code examples
javastruts2elognlvaluestack

How data travels in Struts2?


I am trying to understand the path data takes within Struts2, how is data placed on the ValueStack? How can I find out which object is currently present in the ValueStack? What other data can I access from different scopes application, session, request, page? How to decide the scopes my variables should have?


Solution

  • That's a lot of questions.

    The value stack is just a data structure, sort of a combination of a map and stack. Named objects (accessed via the # tag in OGNL) are in the map (like the request scope, say), objects to search through for properties/methods are on the stack.

    The <s:debug> tag is the easiest way to find out what's in the value stack. You can also access arbitrary objects on the stack using "bare" array notation in JSP, like "[0]" is the top-most object, "[1]" is the next, etc. This is almost never a good idea in real life.

    You can access whatever is in each of the scopes.

    Your own objects should almost always be placed on the value stack via the action itself, or if you're implementing ModelDriven, via the model. Other than that it's the same as any other Java EE application--objects needed for the duration of the client's session should be in session scope, objects shared across the application should be in the application scope, etc.

    The value stack itself is in the request scope.