Navigation Bar

Friday, August 1, 2025

Jakarta Struts : Implementation of MVC

 The Struts Framework uses the MVC design pattern using a combination of custom JSP tags and a JAVA servlet. 

In this article, I will describe how the Struts Framework maps to each component of the MVC.

From the frontend View page, a request is made for a particular Action.

This request is received from the ActionServlet, which acts as the Controller, and the ActionServlet looks up the requested URI in an XML file and determines the name of the Action Class that will perform the necessary business logic.

The Action class performs its logic on the Model Components associated with the application.

Once the Action classs has completed its processing, it returns control to the ActionServlet. As part of the return, the Action class provides a key that indicates the results of its processing. The ActionServlet uses this key to determine where the results should be forwarded for presentation.

The request is completed when the ActionServlet responds by forwarding the request to the View that was linked to the returned key, and this View presents the results of the Action.

       The MVC Model



The Jakarta Struts architecture using MVC



The Model

The model components of the Struts Framework represent the data objects of the Struts application. They often represent business objects like JavaBeans, Enterprise JavaBeans (EJBs) or object representations of data stored in a relational database. Basically anything that needs to be manipulated and presented to the front end Web application as per the client request.

The View

The view in the Struts Framework is mapped to a JSP that can contain a combination of HTML, JSP and Struts custom tags. JSPs in the Struts Framework act as a presentation layer for the response of a previously executed Controller Action. The JSPs also gather the data from the View and pass it to the Controller for a particular controller action. The Struts View contains several Struts specific tags and classes. The below code snippet contains an example of a struts view.


<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="/loginAction.do" name="loginForm" type="com.wrox.loginForm" method="post">
    User Id: <html:text property="username"/><br/>
    Password: <html:password property="password"/><br/>
    <html:submit value="Login"/>
</html:form>

In the sample code above, we can see that several JSP custom tags are being leveraged. The tags are defined in the Struts Framework and provide a loose coupling to the Controller of the Struts application.

The Controller
The controller is the backbone of all Struts applications. It is implemented using org.apache.struts.action.ActionServlet. This servlet receives HTTP service requests and delegates control of each request, based on the URI of the imcoming request, to a user defined org.apache.struts.action.Action class. This Action class is where the model of the application is retrieved and modified. Once the action class has completed its processing, it returns a key to the ActionServlet. This key is used to determine the view that will present the results of the Action class's processing and output. Thus the ActionServlet takes named requests for services and based on these requests creates Action objects to perform the business logic required to complete the service request.


DirectoryContains
/webappThe root directory of the web application. All JSP and HTML files are stored here
/webapp/WEB-INFThis directory contains all resources related to the application that are not in the document root of the application. It is where the web application deployment descriptor is located
/webapp/WEB-INF/classesThis is where the servlet and other classes are located.
/webapp/WEB-INF/libThis directory contains Java Archive (JAR) files that the web application is dependent on


References 


God's Word for the day
Do not find fault before you investigate
  examine first and then criticize
Do not answer before you listen
  and do not interrupt when another is speaking.
Do not argue about a matter that does not concern you
  and do not sit with sinners when they judge a case
Sirach 11:7-9

One does not live by bread alone, 
  but by every word that comes from the mouth of God
Mathew 4:4

Author's Note
Web applications allow compiled classes to be stored in both /WEB-INF/classes and /WEB-INF/lib directories. The class loader will load classes from the /classes directory first, followed by JARs in the /lib directory. It you have duplicate classes in the /classes and /lib directories, the classes in the /classes directory will take precedence.
 -- James Goodwill, Richard Hightower

No comments:

Post a Comment