By: Barbara Viewed: 153338 times Printer Friendly Format
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.
Most Viewed Articles (in Struts )
Latest Articles (in Struts)
Comment on this tutorial
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews