Struts Classes

By: Barbara Emailed: 1586 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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:

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

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

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

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

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


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

Excellent!!!!!!!!!!!!

View Tutorial          By: Ramana at 2009-01-02 19:31:58
2. View Comment

the way of explanation is really good. there is a correction in it. u have mentioned "ActionForms can be used as a convenient way to move data around". Its not meant for that. if u want to move around the data, u can use either value objects(VO) or data transfer objects(DTO).

anyway kudos to the author. good content.


View Tutorial          By: Arul at 2009-01-09 00:13:46
3. View Comment

Excellent!!!!!!!!!!

Tons of thanks to Barbara

Thanks Ramana


View Tutorial          By: Sara at 2009-06-03 05:38:59

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Barbara
Configuring JDBC DataSources in Struts
What are the different scopes in JSP?
Drawbacks of the JSP Model 1 Architecture
Struts Classes

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
Struts 1 vs Struts 2
ForwardAction in Struts
ActionErrors and ActionError in Struts
Using JavaScript to submit a form in Struts
Model 1 Architecture
Introduction to Struts Architecture
DispatchAction in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
MVC Architecture (Model 2 Architecture)
Configuring JDBC DataSources in Struts
Struts and Tiles - Steps to use Struts and Tiles
Most Emailed Articles (in Struts)
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
Guidelines for Struts Application Development
MVC with configurable controller
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
ForwardAction in Struts
Using JavaScript to submit a form in Struts
Simple example of using the requiredif Validator rule in Struts