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>
Directory | Contains |
---|---|
/webapp | The root directory of the web application. All JSP and HTML files are stored here |
/webapp/WEB-INF | This 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/classes | This is where the servlet and other classes are located. |
/webapp/WEB-INF/lib | This directory contains Java Archive (JAR) files that the web application is dependent on |
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.
No comments:
Post a Comment