Working with Structs

Struct is a framework which was released from Jakarta project for java servlet applications based upon the Model-View_Controller (MVC) design pattern. It replaces MVC architectures' model and controller which implies that you download struct framework, you have these model and controller stuff with your struct framework. For now it is a kettle of fish, but sooner or later you will begin to love struct by seeing its capability in aspect of modularity and loosely coupling.

Figure 2.1

As you can see controller consists of servlet java class and model consists of beans and other java classes. But after all struct becomes a compression of above mentioned servlet and java files.
  • controller
  • business classes (java beans in model classes)
  • libraries used in jsp

Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP.

Model view controller improve loosely coupling since Changes in one component will have no or less impact on other component

Model: Model is responsible for providing the data from the database and saving the data into the data store. All the business logic are implemented in the Model. Data entered by the user through View are check in the model before saving into the database. Data access, Data validation and the data saving logic are part of Model.

View: View represents the user view of the application and is responsible for taking the input from the user, dispatching the request to the controller and then receiving response from the controller and displaying the result to the user. HTML, JSPs, Custom Tag Libraries and Resources files are the part of view component.

Controller: Controller is intermediary between Model and View. Controller is responsible for receiving the request from client. Once request is received from client it executes the appropriate business logic from the Model and then produce the output to the user using the View component. ActionServlet, Action, ActionForm and struts-config.xml are the part of Controller.

Deployment Descriptor

WEB-INF/web.xml. This is the file which the container reads.

This file has all the configuration information which we have defined for our web application.

index file

default welcome page

mapping of our servlets including path and the extension name

any init parameters

In the file WEB-INF/web.xml of struts application we need to configure the Struts ActionServlet which handles all the request made by the web browsers to a given mapping.

ActionServlet is the central component of the Struts controller.

This servlet basically performs two important things

When the container gets start, it reads the Struts Configuration files and loads it into memory in the init() method

It intercepts the HTTP request in the doGet() and doPost() method and handles it appropriately

struts.config.xml.

This is a Struts configuration file whose name can be configured in the web.xml file

Placed under the WEB-INF directory.

This file is used to associate paths with the controller components of your application., known as Action classes like

This tag tells the Struts ActionServlet that whenever the incoming request is http://myhost/myapp/login.do, then it must invoke the controller component LoginAction

The model which we want to use is entirely to you, the model is called from within the controller components.

Action can also get associate with a JavaBean in our Struts configuration file

Java bean is nothing but a class having getter and setter methods that can be used to communicate between the view and the controller layer.

These java beans are validated by invoking the validate() method on the ActionForm by the help of the Struts system.

The client sends the request by the normal form submission by using Get or Post method, and the Struts system updates that data in the Bean before calling the controller components.

Process flow:

web.xml : Whenever the container gets start 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 request to the correct action Servlet. ActionServlet is configured as a servlet in the web.xml file

The Controller : This is the heart of the container. Most Struts application 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 reads the struts.config.xml file upon start up. This config file is used to map the request to some destination servlet or jsp file.

Model: Contains the business logic. Struts provides the ActionForm and the Action classes which can be extended to create the model objects.

View:

Struts tag libraries : These are struts components helps 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 : To store the string data, it is used property files.

Web.xml

Understanding Struts Controller



action
org.apache.struts.action.ActionServlet


action
*.do


config
/WEB-INF/struts-config.xml


debug
2


detail
2

2

The above mapping maps all the requests ending with .do to the ActionServlet. ActionServlet uses the configuration defined in struts-config.xml file to decide the destination of the request. Action Mapping Definitions (described below) is used to map any action. For this lesson we will create Welcome.jsp file and map the "Welcome.do" request to this page.

Forwarding the Welcome.do request to Welcome.jsp

The "Action Mapping Definitions" is the most important part in the struts-config.xml. This section takes a form defined in the "Form Bean Definitions" section and maps it to an action class.

Following code under the tag is used to forward the request to the Welcome.jsp.

path="/Welcome"
forward="/pages/Welcome.jsp"
/>

Above part is in struts-config.xml

To call this Welcome.jsp file we will use the following code.

<html:link page="/Welcome.do">First Request to the controller

Once the use clicks on on First Request to the controller link on the index page, request (for Welcome.do) is sent to the Controller and the controller forwards the request to Welcome.jsp. The content of Welcome.jsp is displayed to the user.

What is Action Class?

An Action works as an adapter between the contents of an incoming HTTP request and the business logic that corresponds to it. Then the struts controller (ActionServlet) slects an appropriate Action and creates an instance if necessary, and finally calls execute method.

To use the Action, we need to Subclass and overwrite the execute() method. In the Action Class don't add the business process logic, instead move the database and business process logic to the process or dao layer.

The ActionServlet (commad) 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.

Developing our Action Class?

Our Action class (TestAction.java) is simple class that only forwards the TestAction.jsp. Our Action class returns the ActionForward called "testAction", which is defined in the struts-config.xml file (action mapping is show later in this page). Here is code of our Action Class:

TestAction.java

package roseindia.net;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("testAction");
}
}

Adding the Action Mapping in the struts-config.xml
To test the application we will add a link in the index.jsp
Test the Action

Following code under the tag is used to for mapping the TestAction class.

path="/TestAction"

type="roseindia.net.TestAction">

To test the new application click on Test the Action link on the index page. The content of TestAction.jsp should be displayed on the user browser.



Comments

Popular Posts