how many values available for scope attribute in action element of struts-config.xml file

Manu picture Manu · Jan 17, 2011 · Viewed 9.3k times · Source

How many values available for "scope" attribute in "action" element of struts-config.xml file other than "request" and "session"?

<action name="loginform" path="/bkplogin" scope="?" type="org.springframework.web.struts.DelegatingActionProxy">

Answer

user159088 picture user159088 · Jan 17, 2011

There are only two possible values for the scope attribute: request and session as stated in the struts-config’s DTD:

<!-- 
The name of a JSP bean scope within which such a form bean may be accessed.
-->
<!ENTITY % RequestScope "(request|session)">
...
...
<!ATTLIST action   scope    %RequestScope;  #IMPLIED>

See the DTD here:
http://struts.apache.org/dtds/struts-config_1_3.dtd

or a more human readable documentation of the DTD here:
http://struts.apache.org/1.x/struts-core/dtddoc/struts-config_1_3.dtd#action

What about the "Application" and "page" ?

Well, the scope of an object across JSP pages can be:

  • page - an object can be accessed only from within the same JSP page it was created in;
  • request - objects created using the request scope can be accessed from any pages that serves that request;
  • session - an object is accessible from pages that belong to the same session (spans across multiple requests of the same client, with state maintained in the session, each client with his own session);
  • application - objects from this scope can be accessed from any pages in the application (all users share the same objects in application scope, one object to all users).

Now, the scope in struts-config refers to where to create/find ActionForm objects. An ActionForm represents the server object representation of a client HTML form.

It does not make any sense to have the form with application scope because it will be one form for everybody which which I can't even think at what that will be useful. So no application value for that field.

Now imagine you have page scope. How will that work? Struts does a RequestDispatcher.forward/redirect to go to the JSP files, how is it going to save the ActionForm in the page scope of the page that still does not have a page scope since it does not yet have control?! Is just like sending values to a method but instead of sending method arguments you are trying to directly create local variables in the code of the method from outside the method.

So there are only two values that make sense: request and session. If you want something extra you have to manage it yourself.

Struts is a generic framework, it does not cover every imaginable or unimaginable case, it covers most of normal use case scenarios for which request and session is all you will ever need.