Struts

Define Core classes of Struts Framework
Previous Home Next

The Core classes of Struts Framework are

  • Action
  • ActionServlet
  • ActionForm
  • ActionMapping
  • ActionForward
  • PlugIn Classes

Action class in struts

The Action Class is part of the Model and is a wrapper around the business logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic. In the Action Class all the database/business processing are done. It is strongly recommended to perform all the database related stuffs in the Action Class. The ActionServlet (org.apache.struts.action.ActionServlet) passes the parameterized class to Action Form using the execute() method. The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object. The action class define a method that executed could be depending upon servlet environment.

/*ThisistheactioncalledfromtheStrutsframework.
*@parammappingTheActionMappingusedtoselectthisinstance.
*@paramformTheoptionalActionFormbeanforthisrequest.
*@paramrequestTheHTTPRequestweareprocessing.
*@paramresponseTheHTTPResponseweareprocessing.
*@throwsjava.lang.Exception
*@return
*/
@Override
publicActionForwardexecute(ActionMappingmapping,
ActionFormform,HttpServletRequestrequest
,HttpServletResponseresponse)throwsException
}

 

The main goal of an Action class is to process a request, via its execute() method, and return an ActionForward object that identifies where control should be forwarded (e.g. a JSP, Tile definition, Velocity template, or another Action) to provide the appropriate response.

Action Class design

Remember the following design guidelines when coding Action classes:

  1. Design Action class for multi-threaded environment : In MVC/Model 2 architected, Controller servlet creates only one instance of your Action class, and uses this one instance to service all requests. Then thread-safe Action classes is needed however, two general guidelines that will help you write scalable, thread-safe Action classes:
    • Use Local Variables : An Action can be factored into several local methods, so long as all variables needed are passed as method parameters. This assures thread safety, as the JVM handles such variables internally using the call stack which is associated with a single Thread this is the most important principle that aids in thread-safe coding.
    • Always Conserve Resources : As a general rule, allocating scarce resources and keeping them across requests from the same user (in the user's session) can cause scalability problems. For example, if your application uses JDBC and you allocate a separate JDBC connection for every user, you are probably going to run in some scalability issues when your site suddenly shows up on Slashdot. You should strive to use pools and release resources (such as database connections) prior to forwarding control to the appropriate View component -- even if a bean method you have called throws an exception.
  2. Exception don't throw it, catch it: If application/project specific code throws any exceptions, should be catch these exceptions in your Action class, log them in your application's log (servlet.log("Error message", exception)) and return the appropriate ActionForward.
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: