hey i am new to struts and following is my understanding.. Please correct me whereever possible
In a simple struts application , when the controller gets a request it creates a request object (containing the HTTP method, HTTP protocol and URI) and a response object.
It then checks the mapping files and decides which action class to instantiate..(Its Just one instance of every action irrespective of the number of requests coming in ?? right? ).
The mapping file is then again checked for the corresponding form-bean element.....what does it do then ..?? I get confused with the part...does it invoke a different form instance for each request or uses the same form instance..... Please can someone clarify the processing.
for an existing code ...in the execute declaration i have
Struts is a framework where many things are taken care by the "controllers" and the developers can focus on the business logic.
For a request, the Form Class (extends ActionForm/ ValidatorForm) is set with the values and then the form is passed to the controller Action Class (which extends Action). (ofcourse you have to cast the form and use it!!).
Now which JSP has which Form Class is "told" by the struts-config.xml. (in <form-beans> . Then if any <form action> is called then again struts-config.xml is checked for the mapping and then the corresponding action class is called.
Utimately, when the request reaches the Action class, the form has already been populated with the values. We just have to type cast it and use it. Then based on the logic there are some forwards(mapping.findForward("some-name")) which are again checked for, in the struts-config.xml and the page it forwarded to that particular JSP.
Hope it helps to clear some of your doubts.
posted 12 years ago
Hey Roshani, Thanks for the prompt reply, that does help...
But i still have one question .....
Following are the points mentioned in Oreilly's Programming jakarta struts text....
The following steps illustrate how the framework processes ann ActionForm for every request: 1. Check the mapping for the action and see if an ActionForm has been configured. 2. If an ActionForm is configured for the action, use the name attribute from the action element to look up the form bean configuration information.
3. Check to see if an instance of the ActionForm already has been created.
4. If an ActionForm instance is present in the appropriate scopee and it's the same type as needed for the new request, reuse it. 5. Otherwise, create a new instance of the required ActionForm & store it in the appropriate scope (set by the scope attribute for the action element). 6. Call the reset( ) method on the ActionForm instance.
What does scope and type refer to in the 4th point?? There is just one instance for the Actionform for a particular url type. right??
There will be just one instance of the actionform for all URLs of this type right... I mean if another request http://localhost:8080/MYapp/doThis.do?a=40&b=50&c=60 comes in a new ActionForm instance will not be created? Instead the actionform object passed to the Action Class will have new values for a,b,c right??
"Scope" refers to one of the four scopes in J2EE Web Applications (page, request, session, and application). However, the only scopes that make any sense for an ActionForm are request and session.
If you specify "request" in your action mapping, Struts will create a new ActionForm for every request, since the one used by the previous request is out of scope.
If you specify "session" scope, Struts will look in the session first to see if it's already there. If it is there, Struts will use the existing object. If it isn't there Struts will instantiate a new one. The comment about "type" simply means that Struts will verify that the found object is of the type you specified in the action mapping in your struts-config.xml file. If you've coded your ActionForms and Action Mappings in struts-config.xml properly (without duplicate ActionForm names), it will be. [ September 25, 2006: Message edited by: Merrill Higginson ]
Also keep in mind that session scope is per user. If you have 1000 users hitting the same URL/Action each of those users will have their own session, thus there will be 1000 different instances of your form bean (and only 1 instance of your action).