开发者

Difference between Session persistence vs. Maintaning in Value Stack

I need to persist an object over multiple requests in my Struts 2 application. Is it better to put the object into Session or maintain it on Struts 2's Value Stack?

So, is there a real difference (performance or otherwise) between persisting an object in the session vs. maintaining the object on the Value Stack?

1) Session

mutableSDO = getSession().get(SESSION_OBJ_IDENTIFIER);

// manipulate object

getSession().put(SESSION_OBJ_IDENTIFIER, mutableSDO);

2) Maintain in value stack

<s:property name="mutableDTO"/>
...

// mutableDTO instance variable set by Struts 2, value from client submit action

// mainpulate开发者_StackOverflow社区 mutableDTO

this.mutableDTO = changedDTO;

// jsp uses changed DTO
...
<s:property name="mutableDTO"/>


In S2 application, session, request, and parameters are all part the value stack.

Request scope is exposed by default but you need # to access the other scopes.

If you prefix an attribute with # it will access the aforementioned scopes in the order they appear until the attribute is found. It multiple attributes of the same name appear in the stack of scopes you will need to explicitly look up the required scope with #scope_name.attr.

Between your option 1 and option 2. Only option 1 will persist over multiple requests (actually all requests that maintain a session). The second does not maintain the value over any time but one request, bubbling it forward from one request to the next. The first will let a user go to any page in your application randomly and be able to access the sored value, the second depends on forwarding to the correct pages.

Say you have populated a table of employees and want to go to a details page of the employee, you should only have the employee id in request scope. Even if you need to use emp_id multiple times you should not have it in session scope. So if you are chaining forward and have the data in hand this is the right way to go.

If you have some user preferences say, backgroud color. Then this would be a fine thing to put into a session value because a group or all pages will use it.

The main consideration is if the user opens multiple windows and hits the refresh button, will the site behave as expected? Consider a user opening multiple employee details windows, if the value were loaded as a session value instead of passed as get parameters (which are in the request scope), a user hitting refresh to see the latest employee data on each window would only get the last employee loaded on the session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜