Presentation on theme: "UNIT-V The MVC architecture and Struts Framework."— Presentation transcript:
UNIT-V The MVC architecture and Struts Framework
Agenda Introduction to MVC architecture. Introduction to Struts Framework. Anatomy of simple struts Application. Struts-config.xml file. Presentation layer with JSP. Struts Controller Class, JSP bean, HTML and logic tag libraries. Action forms, Dynamic action form, Actions, forwarding. Simple application using struts framework
Introduction to MVC architecture Model–view–controller (MVC) is a software architecture, currently considered an architectural pattern, used in software engineering. The pattern isolates domain logic (the application logic for the user) from the user interface (input and presentation), permitting independent development, testing and maintenance of each (separation of concerns).
Use of the MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
What is framework and why Framework is set of reusable software program that forms the basis for an application. Frameworks helps the programmers to build the application quickly. Earlier it was very hard to develop complex web applications. Now its very easy to develop such application using different kinds of frameworks such as Struts, Struts 2, Hibernate, JSF, Tapestry, JUnit, Log4j, Spring etc.
Software framework Framework software in computer system is a layered structure that indicates what kind of program should be built and how they would interrelate to one another. In computer system, a framework is a conceptual structure that sometime includes actual programs, which specify programming interface or offer programming tools for using the frameworks.
Struts framework- prerequisite HTML, Java Script JSP Servlet Java Bean Tag Libraries under JSP XML
Introduction to Struts Framework Struts framework: is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model-view-controller (MVC) architecture.open-sourceweb application frameworkJava EEweb applicationsJava ServletAPImodel-view-controller
The goal of Struts is to separate the model (application logic that interacts with a database) from the view (HTML pages presented to the client) and the controller (instance that passes information between view and model). Struts provides the controller (a servlet known as ActionServlet) and facilitates the writing of templates for the view or presentation layer (typically in JSP, but XML/XSLTand Velocity are also supported).XMLXSLTVelocity
Struts framework and 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
Struts framework and 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.
Struts framework and 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.
The web application programmer is responsible for writing the model code, and for creating a central configuration file struts- config.xml that binds together model, view and controller.
Requests from the client are sent to the controller in the form of "Actions" defined in the configuration file; if the controller receives such a request it calls the corresponding Action class that interacts with the application-specific model code.
The model code returns an "ActionForward", a string telling the controller what output page to send to the client. Information is passed between model and view in the form of special JavaBeans. A powerful custom tag library allows it to read and write the content of these beans from the presentation layer without the need for any embedded Java code. Struts is categorized as a request-based web application framework.
Struts framework provides three key components: 1.A request handler provided by the application developer that is used to mapped to a particular URI (Uniform Resource Identifier). 2.A response handler which is used to transfer the control to another resource which will be responsible for completing the response. 3.A tag library which helps developers to create the interactive form based applications with server pages.
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. A 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.
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.
Model : The model is basically a business logic part which takes the response from the user and stores the result for the duration of the process. This is a great place to perform the preprocessing of the data received from request. It is possible to reuse the same model for many page requests. Struts provides the ActionForm and the Action classes which can be extended to create the model objects.
View : The view in struts framework is mainly a jsp page which is responsible for producing the output to the user. 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 : 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 exists. These are the modules which just regulate the day- to- day site activities. The Response : This is the output of the View JSP object.
Let us work with simple application using struts framework.