Search code examples
javaconfigurationstruts2migrationstruts

Struts 2.3: Change scope for Action Beans


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?


Solution

  • 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.