By: Barbara in Struts Tutorials on 2008-12-09
As you'd expect in an MVC framework, every request starts at the Struts controller, the heart of which is the ActionServlet class. This servlet is usually mapped to the extension *.do. For example, a request for /process.do would end up with the ActionServlet and tell it that the user has submitted a pizza order:
The controller servlet first determines what needs to be done. For every possible action uniform resource locator (URL), such as /pizza.do, it determines what needs to happen next. This information is read from Struts configuration files at runtime into instances of the ActionMapping class. So the first thing the controller servlet does is call ActionMappings.findMapping() to find what the appropriate ActionMapping is for the request URL (/process.do in this example).
One of the things you often need to do in Web applications is store field values from an HTML form in a JavaBean. One of the things an action mapping can do is specify an ActionForm associated with the action. An ActionForm is a form-specific JavaBean that represents the input data from an HTML form. The controller servlet automatically instantiates this bean if necessary and populates it with request parameter values.
Pretend that /process.do is invoked by an order page asking the user for a pizza order. The HTML form on this page has a field called size. If you supply Struts with an ActionForm that has a size JavaBean property, it will automatically populate that property with the size entered by the user.
ActionForms may be part of the application model, or they can be used as a convenient way to move data around.
Optionally, the controller servlet can ask the ActionForm to validate the data that has been submitted by calling validate() on the form. If the ActionForm indicates that validation fails, the request is forwarded back to the input HTML page with an error message.
For example, the ActionForm could verify that a name has been entered; if it were missing, the request would be forwarded back to the original form with a Please enter your name message.
The next step is to invoke the Action object for the request URL. The Action classes provide the application-specific business logic necessary to process the request. You'll generally write one Action class for every different action in the application.
A good Action class is small, focused, and easy to understand; it's merely the glue between the controller and the model. Actions aren't a good place to implement complicated business logic. An action will generally call one or more business methods in the model to get the actual work done.
In the /process.do example, the action would initiate the actual order placement.
After the action has been completed successfully, the controller forwards the request to the view component. This view is usually a JSP page. The JSP request is forwarded as configured in the Struts configuration file and-you guessed it-contained at runtime in the ActionMapping object. These destinations are represented by ActionForward objects.
Struts allows you to set up multiple destinations for a single action, which is useful if an action can have multiple outcomes. For example, writing data to a database could succeed or could fail. You'll probably want to forward to different JSP pages depending on the outcome.
The view JSP page can either access the Model directly, using <jsp:getProperty> or the Expression Language (EL), or use the Struts tag libraries to retrieve data from the model. For example, if you use the tags in the Struts html tag library to create HTML forms, the tags will automatically populate the form fields with the values in your ActionForm.
In broad terms, this is how Struts handles a request.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Struts )
Latest Articles (in Struts)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews