Struts Basics


Why do we need to go struts.?
Normal Java EE web applications works in this form.

Flow of Servlet & JSP Web Applications
drawbacks

  • difficult to maintenance.
  • application logic mingled with presentation.(inadequate for large projects).

Struts

Its an open-source framework for developing the web applications in Java EE. The goal of struts is to cleanly separate Business logic from User Interface and Program flow using MVC(Model View Controller) design pattern.

MVC Architecture

MVC Design Pattern Architecture

Image Courtesy : Sun/Oracle

Model – The model represents the data and the business logic to process the data of an application. Any data that an application will persist becomes a part of model.

View – The view represents the presentation of the application. The view can get data from the model and decide on how to present them to the client.

Controller – The controller intercepts the requests from view and passes it to the model for appropriate action. It also decides on the next view to be presented depending on the last client action and the outcome of the model operations.

Struts component classes

ActionServlet
ActionServlet is a backbone of all struts applications. This class plays the role of Controller and is responsible for handling all requests. All incoming requests are mapped to the central controller in the deployment descriptor.

Action
Action is a part of the Model and is a wrapper around the business logic.Purpose of this class is to translate the Http servlet request to the business logic that should be executed to process the request. It can also perform Authorization, Logging before invoking business operation.

execute() is the important method in this class. This has been called by the controller when a request is received from the client, process the request and return to an appropriate action forward.

ActionForm
ActionForm is a Java bean, maintains the session state for web applications. It has setters/getters method used to capture input data from an HTML form(View) and transfer it to the Action class. It also used to transfer data from the Action class back to the HTML form. It also a place to put data conversion and error handling logics.

Struts Configuration File
struts-config.xml is a central configuration file binds together model, view and controller. Five important sections/definitions of config file as follows.

Form bean Definitions
You will write one of these for each HTML form your app needs to process.

 <form-beans>
 <form-bean name="BookForm" type="com.book.BookForm" />
 </form-beans>
 

Global Forward Definitions
A set of forward elements describing generally available forward URIs.

 <global-forwards>
 <forward name="logon" path="/books/jsp/logon.jsp" />
 </global-forwards>
 

Action Mappings
It contains the mapping from URL path to an Action class and also have association with Form Bean. A set of action elements describing a request to action mapping.

<action-mappings>
<action path="/books"
type="com.book.action.BookAction"
name="BookForm"
validate="false"
scope="session">
<forward name="success" path="/books/success.jsp"/>
<forward name="failure" path="/books/failure.jsp"/>
</action>
</action-mappings>

type – fully qualified class name of action.
name – name of the form bean associated with this action.
scope – specifies how long the form bean should live, either session or request.
validate – if true form bean is validated on submission.Otherwise validation is skipped.
forward – the page of which the control should be forwarded, these are all local forwards can be accessed only within the ActionMapping.

Controller Configurations
Its an optional one, default controller is org.apache.struts.action.RequestProcessor.

ActionServlet still receives the requests, but then delegates the request handling to an instance of the RequestProcessor class that has been installed. You can subclass the RequestProcessor with you own version and modify how the request is processed.

<controller processorClass="org.apache.struts.action.RequestProcessor" />

Message Resources
It has built-in support for Internationalization(I18N). You can also define different resource bundles simulatenously in your web application.

<message-resources parameter="resources/ApplicationResources" />

parameter – name of the resource bundle. Optional attributes are className, factory, key, null, escape

Configuring struts application in Deployment Descriptor
We have to configure about ActionServlet in web application. This can be done by adding the following servlet definition in web.xml file.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Servlet Mappings tells when the action should be executed. All request URIs with the pattern *.do are mapped to this servlet. ie) specified servlet action should service all the requests with URL pattern *.do.

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Advantages of Struts

  • Enables clean separation of code between functionality and presentation.
  • Built in internationalization – have the capability to display input in multiple languages.
  • Built in extensible authentication and validation.
  • Promotes modularity ie) Allows modular development and easy integration with new components.
  • Allowing developer to concentrate on the Business Logic.
  • More easily maintainable and extensible.(reduce the code complexity and duplication).

See the complete example of Struts App(Book Store) here : https://manikandanmv.wordpress.com/2010/12/07/struts-example/

Advertisements

Tags: , , , , , , , , , , , , , , , , , , , ,

One Response to “Struts Basics”

  1. The Best Web Design Company in Dubai Says:

    Hello! I’m at work browsing your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the excellent work!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: