Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. MVC <ul><li>Introduction </li></ul><ul><li>Architecture ( Model diagram) </li></ul><ul><li>MVC using JSP / Servlet / Bean </li></ul><ul><li>Struts </li></ul>
  2. 2. What are design patterns? <ul><li>a design pattern is a </li></ul><ul><ul><li>General solution to a commonly-occurring problem </li></ul></ul><ul><ul><li>is a template for solving a problem that can be used in many different problems or solutions. </li></ul></ul>
  3. 3. About MVC <ul><li>Model-View-Controller is made up of </li></ul><ul><ul><li>a number of smaller design patterns. </li></ul></ul><ul><ul><li>is an aggregate design patterns </li></ul></ul><ul><ul><ul><li>So, we call it as architectural pattern. </li></ul></ul></ul><ul><li>MVC pattern is basically layered architecture for a Web application. </li></ul><ul><ul><li>It evolves from front controller pattern </li></ul></ul>
  4. 4. Front controller pattern <ul><li>defines a component </li></ul><ul><ul><li>It processes application requests. </li></ul></ul><ul><ul><li>It centralizes functions like </li></ul></ul><ul><ul><ul><li>view selection, security, and templating, and applies them consistently across all pages or views. </li></ul></ul></ul><ul><ul><ul><li>If behavior of these functions changes, </li></ul></ul></ul><ul><ul><ul><ul><ul><li>Only the controller and its helper changes </li></ul></ul></ul></ul></ul>
  5. 5. Problem domain <ul><li>In a applicationm, financial services over the web. </li></ul><ul><ul><li>Visitors to this site can browse through </li></ul></ul><ul><ul><ul><li>the list of services, create accounts, place orders for the products made available by the financial service, and so on. customers can change their account details and profile, make use of services, and so on. </li></ul></ul></ul><ul><ul><li>The business logic represents the user's account, profile, the catalog of services, ordering for services, and so on, as separate entities in the form of Enterprise JavaBeans (EJB). </li></ul></ul><ul><ul><li>We can identify some recurring problems and apply specific patterns </li></ul></ul><ul><ul><ul><li>to build a flexible and maintainable application. </li></ul></ul></ul>
  6. 6. What is MVC? <ul><li>Model-View-Controller architecture is used for developing web-applications. </li></ul><ul><li>This model is for decoupling between business logic and data presentation to web user. </li></ul><ul><ul><li>Thus model divides the web based application into three layers: </li></ul></ul><ul><ul><ul><li>Model: </li></ul></ul></ul><ul><ul><ul><ul><li>contains the business logics and functions that manipulate the application data. </li></ul></ul></ul></ul><ul><ul><ul><li>View: </li></ul></ul></ul><ul><ul><ul><ul><li>View is responsible for presentation aspect of application according to the current state of model data and query response to the controller. </li></ul></ul></ul></ul><ul><ul><ul><li>Controller: </li></ul></ul></ul><ul><ul><ul><ul><li>Controller accepts and intercepts user requests and controls the business objects to fulfill these requests. </li></ul></ul></ul></ul>
  7. 7. A variation of MVC architecture
  8. 8. Model Layer <ul><li>Designed for </li></ul><ul><ul><li>capturing logic and </li></ul></ul><ul><ul><li>data access code </li></ul></ul><ul><li>Model layer is a self contained </li></ul><ul><li>Its functions are independent from the view and control layer </li></ul><ul><li>Mostly implemented using technologies </li></ul><ul><ul><li>Enterprise JavaBeans, Java Data Objects, Data Access Objects </li></ul></ul>
  9. 9. View Layer <ul><li>Designed for </li></ul><ul><ul><li>providing an interface to application for users </li></ul></ul><ul><li>It is the conduit </li></ul><ul><ul><li>for getting data in and out of the application </li></ul></ul><ul><li>It does not contain any code for </li></ul><ul><ul><li>persisting data to or </li></ul></ul><ul><ul><li>retrieving data from a data source. </li></ul></ul><ul><li>Mostly implemented using technologies </li></ul><ul><ul><li>HTML/JSP,XML/XSLT, Velocity, Swing </li></ul></ul>
  10. 10. Controller layer <ul><li>Designed as a connector between model and View layer, separating one from each other. </li></ul><ul><li>Mostly implemented using technologies : Servlet/JSP. </li></ul>
  11. 11. Advantage of MVC <ul><li>Separation of data from presentation layer </li></ul><ul><li>Ability of have multiple view of single data </li></ul><ul><li>It is less painful to change a data layer or business rules </li></ul><ul><li>Ability to have multiple interface for the same application </li></ul>
  12. 12. Technology and Framework used in MVC <ul><li>Technology </li></ul><ul><ul><li>Servlet /JSP, XML/XSLT, Velocity, Swing </li></ul></ul><ul><ul><li>JavaBean, Java Data Objects, Data Access Objects </li></ul></ul><ul><li>Frameworks </li></ul><ul><ul><li>Struts </li></ul></ul><ul><ul><li>Turbo Gears </li></ul></ul><ul><ul><li>JSF </li></ul></ul>
  13. 13. What is a Servlet? <ul><li>A java program that runs within the web server. </li></ul><ul><li>It acts as controller in MVC architectural pattern </li></ul>
  14. 14. Life of a Servlet Web Browser Web Server Java Servlet Database
  15. 15. Java Server Pages <ul><li>Compiled into Java Servlets by JSP compilers </li></ul><ul><li>Can be used alone or in conjunction with servlets </li></ul><ul><li>Represent (yet) another method for creating server side applications </li></ul><ul><li>It also used for presentation layer along with HTML.(as view layer) </li></ul>
  16. 16. Servlets v. JSP <ul><li>Servlets </li></ul><ul><ul><li>code looks like a regular Java program. </li></ul></ul><ul><li>JSP </li></ul><ul><ul><li>embed Java commands directly within HTML </li></ul></ul><ul><li>Let’s examine a Servlet program next to a JSP program… </li></ul><ul><li>Each of these prints, “Hello, World!” </li></ul>
  17. 17. import*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(&quot;text/html&quot;); PrintWriter out = res.getWriter(); out.println(&quot;<HTML>&quot;); out.println(&quot;<HEAD><TITLE>Hello World</TITLE></HEAD>&quot;); out.println(&quot;<BODY>&quot;); out.println(&quot;<BIG>Hello World</BIG>&quot;); out.println(&quot;</BODY></HTML>&quot;); } } A Java Servlet : Looks like a regular Java program
  18. 18. <html> <head> <title>Hello, World JSP Example</title> </head> <body> <h2> Hello, World! The current time in milliseconds is <%= System.currentTimeMillis() %> </h2> </body> </html> A JSP Page : Looks like a regular HTML page. Embedded Java command to print current time.
  19. 19. Java Beans <ul><li>Definition: </li></ul><ul><li>A Java Bean is a reusable software component that can be manipulated visually using a builder tool. </li></ul>
  20. 20. Java Beans <ul><li>Features that distinguish a Java Bean from Java objects are: </li></ul><ul><ul><li>Support for introspection </li></ul></ul><ul><ul><li>Support for customization </li></ul></ul><ul><ul><li>Support for events </li></ul></ul><ul><ul><li>Support for properties </li></ul></ul><ul><ul><li>Support for persistence </li></ul></ul>
  21. 21. An example Example: The sample application will have a basic opening page that links to an employee search page.From the Employee search page,user can search for employees by name or social security number.After executing the search,the search page will be redisplayed with a list of employees that match the search criteria.
  22. 22. model 2 architecture
  23. 23. Struts <ul><li>It is a framework for </li></ul><ul><ul><li>building java-based Web application </li></ul></ul><ul><ul><ul><li>using the Model-View-Controller (MVC) design pattern. </li></ul></ul></ul><ul><li>It allows us to fully decouple the business logic,control logic, and presentation code of the applications . </li></ul><ul><li>Struts also provides library and utility for making MVC development faster and easier </li></ul>
  24. 24. Struts and MVC mapping <ul><li>Model: </li></ul><ul><ul><li>The action class is where struts framework ends and application code starts. There are some Struts Built in Actions </li></ul></ul><ul><li>View: </li></ul><ul><ul><li>Struts provides presentation features in HTML/JSP. </li></ul></ul><ul><li>Controller: </li></ul><ul><ul><li>The ActionServlet class is the main controller class that receives all incoming HTTP requests. </li></ul></ul><ul><ul><li>Like other Servlet this is configured in the web.xml </li></ul></ul>
  25. 25. Basic component of Struts <ul><li>Base framework </li></ul><ul><li>JSP tag libraries </li></ul><ul><li>Validator plugin </li></ul><ul><li>Tiles plugin </li></ul>
  26. 26. View Layer in Struts <ul><li>JSP pages </li></ul><ul><ul><li>contains the static HTML and JSP library tags that generate dynamic HTML. </li></ul></ul><ul><li>Form Beans </li></ul><ul><ul><li>provide a channel for transferring data between view and controller </li></ul></ul><ul><li>JSP tag libraries </li></ul><ul><ul><li>Glues form beans and resource bundles </li></ul></ul><ul><li>Resource bundles </li></ul><ul><ul><li>Provide a means of content management </li></ul></ul>
  27. 27. Form Beans <ul><li>Struts takes the incoming form data and uses it to populate the form’s corresponding Form Bean </li></ul><ul><li>Controller layer then uses the Form beans to access data that must be sent to the Model Layer and there is also flip side. </li></ul><ul><li>Form beans are simple data containers. </li></ul>
  28. 28. JSP Tag libraries <ul><li>Aids in the development of JSPs </li></ul><ul><ul><li>HTML:generate HTML forms with struts api </li></ul></ul><ul><ul><li>Bean: interact with bean object </li></ul></ul><ul><ul><li>Logic:conditional logic in JSPs </li></ul></ul><ul><ul><li>Nested: to allow arbitrary level of nesting of the HTML,Bean and Logic Beans </li></ul></ul>
  29. 29. Resource Bundles <ul><li>Resource bundles allow Java applications to be easily internationalized by having application content placed into bundles. </li></ul><ul><li>The content can be changed without having to recompile application. </li></ul><ul><li>Reduces duplication </li></ul><ul><li>Used in populate JSP page,customized error message </li></ul>
  30. 30. Controller Layer in Struts <ul><li>ActionServlet class </li></ul><ul><ul><li>all requests received by the ActionServlet </li></ul></ul><ul><li>RequestProcessor class </li></ul><ul><ul><li>Struts uses this class to perform the processing for all requests received by the ActionServlet. </li></ul></ul><ul><ul><li>To use a custom request processor ,we have to configure Struts to use It in the Struts configuration file </li></ul></ul>
  31. 31. Struts Model interface as action class <ul><li>The action class is where struts framework ends and application code starts </li></ul><ul><li>There are some Struts Built in Actions </li></ul><ul><li>Model code accessed from the subclasses of Struts’ Action object </li></ul><ul><li>Action subclasses interact with the Model via Action’s interfaces and uses its Data Transfer Objects to pass and retrieve data </li></ul>
  32. 32. Browser ActionServlet RequestProcessor Action Model View Application execution with Struts Framework
  33. 33. Flow of execution <ul><li>The browser makes a request to the struts application that is processed by ActionServlet (Controller). </li></ul><ul><li>ActionServlet (Controller) populates the ActionForm (View ) object with the HTML form data and invokes its validate() method </li></ul><ul><li>ActionServlet (Controller)executes the Action object(Controller) </li></ul>
  34. 34. Continued…….. <ul><li>Action(Controller) interfaces with the model components and prepares data for view </li></ul><ul><li>Action(Controller) forwards control to the JSP(View). </li></ul><ul><li>JSP(view) uses model data to generate a response to the browser. </li></ul>
  35. 35. example Browser 2.Populate 1.Request 3. Delegate JSP Database/ Services 2. populate 5.Forward 4.inteface Response ActionServlet Action Action Action SearchForm SearchForm SearchForm Model class Model class Model class
  36. 36. References <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li>Struts: The Complete Reference, Tata McGraw-Hill Edition 2004, ISBN 0-07-058861-9 </li></ul>