how to use a single command object in 2 different page without making it singleton
How do I make transfer of a class object from one page to another in a jsp while doing server process too,
eg let be there a page1.jsp it have a commandObject page1 of a class Page
I then fill some of its value in page1.jsp,
I then want the same object to be transfer开发者_开发问答ed to another page say page2.jsp and on that page I fill the remaining values of page1 object and then persist it to data base.
In the post method of your controller for page 1, add your command object to the model again and return the view name for page 2.
It sounds like you are describing a simple conversation workflow. If your workflow becomes more complex I recommend looking at Spring Webflow.
If you don't wan't to (or can't) use a Singleton bean, how about using the request
or session
scopes? They are custom-made for this kind of scenario.
3.5.4.2 Request scope
Consider the following bean definition:
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
The Spring container creates a new instance of the
LoginAction
bean by using theloginAction
bean definition for each and every HTTP request. That is, the loginAction bean is scoped at the HTTP request level. You can change the internal state of the instance that is created as much as you want, because other instances created from the sameloginAction
bean definition will not see these changes in state; they are particular to an individual request. When the request completes processing, the bean that is scoped to the request is discarded.
3.5.4.3 Session scope
Consider the following bean definition:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
The Spring container creates a new instance of the
UserPreferences
bean by using theuserPreferences
bean definition for the lifetime of a single HTTP Session. In other words, theuserPreferences
bean is effectively scoped at the HTTP Session level. As with request-scoped beans, you can change the internal state of the instance that is created as much as you want, knowing that other HTTP Session instances that are also using instances created from the same userPreferences bean definition do not see these changes in state, because they are particular to an individual HTTP Session. When the HTTP Session is eventually discarded, the bean that is scoped to that particular HTTP Session is also discarded.
Source:
- Request, session, and global session scopes
精彩评论