Struts

Index

  1. Introduction to Struts Framework
  2. Architecture of Struts
  3. Struts with Database Connectivity


Struts

Struts is a java based open-sourced framework that helps in developing a web application in J2EE. It extends the java servlet API and promotes the model, view, controller (MVC) architecture. This makes the web applications developed in standard technologies like JSP, java beans, and XML more maintainable, extensible and flexible.

Struts help developers to create flexible, maintainable, and secure web applications in java. As mentioned above, the framework is based on the MVC architecture, where each component takes care of different aspects of the application.

It uses and extends the Java Servlet API to encourage developers adopting MVC architecture. Struts framework provides three key components:
  • A request handler provided by the application developer is used to be mapped to a particular URI.
  • A response handler is used to transfer the control to another resource that will be responsible for completing the response.
  • A tag library that helps developers to create interactive form-based applications with server pages.

Overview of the Struts Framework/ Architecture

The Struts framework is composed of approximately 300 classes and interfaces which are organized in about 12 top-level packages. Along with the utility and helper classes framework also provides the classes and interfaces for working with controller and presentation with the help of the custom tag libraries. It is entirely up to us which model we want to choose. The view of the Struts architecture is given below:
  • The Struts Controller Components: Whenever a user request something, then the request is handled by the Struts Action Servlet. When the ActionServlet receives the request, it intercepts the URL, and based on the Struts Configuration files, it gives the handling of the request to the Action class. Action class is a part of the controller and is responsible for communicating with the model layer.
  • The Struts View Components: The view components are responsible for presenting information to the users and accepting the input from them. They are responsible for displaying the information provided by the model components. Mostly we use the JavaServer Pages (JSP) for the view presentation. To extend the capability of the view we can use Custom tags, javascript, etc.
  • The Struts model component: The model component provides a model of the business logic behind a Struts program. It provides interfaces to databases or back- ends systems. Model components are generally a java class. There is not any such defined format for a Model component, so it is possible for us to reuse Java codes that are written for other projects. We should choose the model according to our client's requirements.

Struts Architecture

The process flow is shown below.

  • web.xml: Whenever the container gets started up the first work it does is to check the web.xml file and determine what struts action Servlets exist. The container is responsible for mapping all the file requests to the correct action Servlet.
  • Request: This is the second step performed by the container after checking the web.xml file. In this, the user submits a form within a browser and the request is intercepted by the controller.
  • Controller: This is the heart of the container. Most Struts applications will have only one controller that is ActionServlet which is responsible for directing several Actions. The controller determines what action is required and sends the information to be processed by an action Bean. The key advantage of having a controller is its ability to control the flow of logic through the highly controlled, centralized points.
  • struts-config.xml: Struts has a configuration file to store mappings of actions. By using this file there is no need to hard code the module which will be called within a component. The one more responsibility of the controller is to check the struts-config.xml file to determine which module to be called upon an action request. Struts only read the struts-config.xml file upon startup.
  • Model: The model is basically a business logic part that takes the response from the user and stores the result for the duration of the process. This is a great place to perform the pre-processing of the data received from the request. It is possible to reuse the same model for many page requests. Struts provide the ActionForm and the Action classes which can be extended to create the model objects.
  • View: The view in the Struts framework is mainly a JSP page that is responsible for producing the output to the user.
  • Struts tag libraries: These are struts components that help us to integrate the struts framework within the project’s logic. These struts tag libraries are used within the JSP page. This means that the controller and the model part can’t make use of the tag library but instead use the struts class library for strut process control.
  • Property file: It is used to store the messages that an object or page can use. Properties files can be used to store the titles and other string data. We can create many property files to handle different languages.
  • Business objects: It is the place where the rules of the actual project exist. These are the modules that just regulate the day-to-day site activities.
  • Response: This is the output of the View JSP object.

Deploying the application in struts with database connectivity

  1. Test the connection within the database and webpage
  2. if the Database connection is not possible (False) then show the Error page and Exit the process
  3. if the Database connection is possible (True) then following these points
    1. Create input/ welcome page
    2. Create action class
    3. create a class to store data.
  4. Process the ResultSet
    1. if return False then Exit and show Error page
    2. if return True then Map the request in file and define the view components.
  5. Create view components/ Display report
Top

Comments

Popular posts from this blog

Servlet

Java Server Pages - JSP