1

I'm new to Struts2.

I cannot find any information in Struts docs about bean's scopes, and Struts uses Request scope by default, which is not suitable for my project.

I found a lot of info about scope usage in Struts1, but all links to docs are unavailable.

Could you please suggest how to set up scope for Action Classes?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Aventes
  • 569
  • 1
  • 8
  • 20
  • You can read more about scopes [here](http://stackoverflow.com/a/27670618/573032) – Roman C Dec 16 '16 at 22:34
  • [This](http://stackoverflow.com/a/20458850/573032) answer is also about scopes but (not upvoted). Regarding your reply *Struts uses Request scope by default*. – Roman C Dec 16 '16 at 22:40

1 Answers1

1

Struts it's a front-end MVC framework.

Struts2 Actions are Controllers (the C of the MVC), and shouldn't be used as beans; they should instead carry beans from the underlying business layers to the View (the V of the MVC, eg. a JSP).

You shouldn't have an action with 50 attributes, you should have an action with a couple of attributes that are POJO with the 50 attributes inside.

Behind Struts2 you can have anything running on the business layer, for example Java EE (EJB 3, JPA 2, CDI, etc...), with all its beans and scopes, or Spring, or whatever.

Struts2 Actions are thread-safe, instantiated per-request, and this is how it is studied to work. Don't try to change actions' scopes to use them as beans, try to learn what actions are and how they should be used.

Sidenote: if you have free of choice, I'd suggest Struts2 + Java EE >=6 + Struts2-CDI-plugin + Struts2-Convention-plugin + Struts2-Json-plugin, run with Maven and created from a Maven archetype.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you for the answer. I have a Struts2 + EJB3 in the project, and I need to load from the db some data to initialize and use on the screen. I cannot do this operation each request - because this operation too expensive. Currently I use session to store screen data - but this is very bad practice... Any suggestions, how to store the data between requests? I think that usage controller as singleton or with scope session - it is very good idea, like servlets or spring mvc controllers.. – Aventes Dec 16 '16 at 19:57
  • If the data is per-user, session is good. If the data is common, cache it, eg. in ejb3 @Singleton. Read more: http://stackoverflow.com/questions/26733141/load-and-cache-application-scoped-data-with-singleton-and-stateless – Andrea Ligios Dec 16 '16 at 22:40