• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MVC
 

MVC

on

  • 2,619 views

 

Statistics

Views

Total Views
2,619
Views on SlideShare
2,616
Embed Views
3

Actions

Likes
2
Downloads
111
Comments
0

1 Embed 3

http://www.blogger.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MVC MVC Presentation Transcript

    • MVC
      • Introduction
      • Architecture ( Model diagram)
      • MVC using JSP / Servlet / Bean
      • Struts
    • What are design patterns?
      • a design pattern is a
        • General solution to a commonly-occurring problem
        • is a template for solving a problem that can be used in many different problems or solutions.
    • About MVC
      • Model-View-Controller is made up of
        • a number of smaller design patterns.
        • is an aggregate design patterns
          • So, we call it as architectural pattern.
      • MVC pattern is basically layered architecture for a Web application.
        • It evolves from front controller pattern
    • Front controller pattern
      • defines a component
        • It processes application requests.
        • It centralizes functions like
          • view selection, security, and templating, and applies them consistently across all pages or views.
          • If behavior of these functions changes,
              • Only the controller and its helper changes
    • Problem domain
      • In a applicationm, financial services over the web.
        • Visitors to this site can browse through
          • 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.
        • 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).
        • We can identify some recurring problems and apply specific patterns
          • to build a flexible and maintainable application.
    • What is MVC?
      • Model-View-Controller architecture is used for developing web-applications.
      • This model is for decoupling between business logic and data presentation to web user.
        • Thus model divides the web based application into three layers:
          • Model:
            • contains the business logics and functions that manipulate the application data.
          • View:
            • View is responsible for presentation aspect of application according to the current state of model data and query response to the controller.
          • Controller:
            • Controller accepts and intercepts user requests and controls the business objects to fulfill these requests.
    • A variation of MVC architecture
    • Model Layer
      • Designed for
        • capturing logic and
        • data access code
      • Model layer is a self contained
      • Its functions are independent from the view and control layer
      • Mostly implemented using technologies
        • Enterprise JavaBeans, Java Data Objects, Data Access Objects
    • View Layer
      • Designed for
        • providing an interface to application for users
      • It is the conduit
        • for getting data in and out of the application
      • It does not contain any code for
        • persisting data to or
        • retrieving data from a data source.
      • Mostly implemented using technologies
        • HTML/JSP,XML/XSLT, Velocity, Swing
    • Controller layer
      • Designed as a connector between model and View layer, separating one from each other.
      • Mostly implemented using technologies : Servlet/JSP.
    • Advantage of MVC
      • Separation of data from presentation layer
      • Ability of have multiple view of single data
      • It is less painful to change a data layer or business rules
      • Ability to have multiple interface for the same application
    • Technology and Framework used in MVC
      • Technology
        • Servlet /JSP, XML/XSLT, Velocity, Swing
        • JavaBean, Java Data Objects, Data Access Objects
      • Frameworks
        • Struts
        • Turbo Gears
        • JSF
    • What is a Servlet?
      • A java program that runs within the web server.
      • It acts as controller in MVC architectural pattern
    • Life of a Servlet Web Browser Web Server Java Servlet Database
    • Java Server Pages
      • Compiled into Java Servlets by JSP compilers
      • Can be used alone or in conjunction with servlets
      • Represent (yet) another method for creating server side applications
      • It also used for presentation layer along with HTML.(as view layer)
    • Servlets v. JSP
      • Servlets
        • code looks like a regular Java program.
      • JSP
        • embed Java commands directly within HTML
      • Let’s examine a Servlet program next to a JSP program…
      • Each of these prints, “Hello, World!”
    • import java.io.*; 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
    • <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.
    • Java Beans
      • Definition:
      • A Java Bean is a reusable software component that can be manipulated visually using a builder tool.
    • Java Beans
      • Features that distinguish a Java Bean from Java objects are:
        • Support for introspection
        • Support for customization
        • Support for events
        • Support for properties
        • Support for persistence
    • 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.
    • model 2 architecture
    • Struts
      • It is a framework for
        • building java-based Web application
          • using the Model-View-Controller (MVC) design pattern.
      • It allows us to fully decouple the business logic,control logic, and presentation code of the applications .
      • Struts also provides library and utility for making MVC development faster and easier
    • Struts and MVC mapping
      • Model:
        • The action class is where struts framework ends and application code starts. There are some Struts Built in Actions
      • View:
        • Struts provides presentation features in HTML/JSP.
      • Controller:
        • The ActionServlet class is the main controller class that receives all incoming HTTP requests.
        • Like other Servlet this is configured in the web.xml
    • Basic component of Struts
      • Base framework
      • JSP tag libraries
      • Validator plugin
      • Tiles plugin
    • View Layer in Struts
      • JSP pages
        • contains the static HTML and JSP library tags that generate dynamic HTML.
      • Form Beans
        • provide a channel for transferring data between view and controller
      • JSP tag libraries
        • Glues form beans and resource bundles
      • Resource bundles
        • Provide a means of content management
    • Form Beans
      • Struts takes the incoming form data and uses it to populate the form’s corresponding Form Bean
      • Controller layer then uses the Form beans to access data that must be sent to the Model Layer and there is also flip side.
      • Form beans are simple data containers.
    • JSP Tag libraries
      • Aids in the development of JSPs
        • HTML:generate HTML forms with struts api
        • Bean: interact with bean object
        • Logic:conditional logic in JSPs
        • Nested: to allow arbitrary level of nesting of the HTML,Bean and Logic Beans
    • Resource Bundles
      • Resource bundles allow Java applications to be easily internationalized by having application content placed into bundles.
      • The content can be changed without having to recompile application.
      • Reduces duplication
      • Used in populate JSP page,customized error message
    • Controller Layer in Struts
      • ActionServlet class
        • all requests received by the ActionServlet
      • RequestProcessor class
        • Struts uses this class to perform the processing for all requests received by the ActionServlet.
        • To use a custom request processor ,we have to configure Struts to use It in the Struts configuration file
    • Struts Model interface as action class
      • The action class is where struts framework ends and application code starts
      • There are some Struts Built in Actions
      • Model code accessed from the subclasses of Struts’ Action object
      • Action subclasses interact with the Model via Action’s interfaces and uses its Data Transfer Objects to pass and retrieve data
    • Browser ActionServlet RequestProcessor Action Model View Application execution with Struts Framework
    • Flow of execution
      • The browser makes a request to the struts application that is processed by ActionServlet (Controller).
      • ActionServlet (Controller) populates the ActionForm (View ) object with the HTML form data and invokes its validate() method
      • ActionServlet (Controller)executes the Action object(Controller)
    • Continued……..
      • Action(Controller) interfaces with the model components and prepares data for view
      • Action(Controller) forwards control to the JSP(View).
      • JSP(view) uses model data to generate a response to the browser.
    • 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
    • References
      • http://java.sun.com/blueprints/patterns/FrontController.html
      • http://java.sun.com/blueprints/patterns/MVC.html
      • http://struts.apache.org/
      • Struts: The Complete Reference, Tata McGraw-Hill Edition 2004, ISBN 0-07-058861-9