MVC pattern in java web programming Aleksandar Kartelj, Faculty of Mathematics Belgrade DAAD workshop Ivanjica 6. -11.9.2010 Serbia September 2010
Outline 1 2 3 4 5 6
History Simple information portals in early nineties Two major standard for building web apps: and Apache Struts (along with J2EE framework) Standard patterns implemented in and Struts have helped reduce code complexity and accelerate development times
History Simple information portals in early nineties Two major standard for building web apps: and Apache Struts (along with J2EE framework) Standard patterns implemented in and Struts have helped reduce code complexity and accelerate development times
History Simple information portals in early nineties Two major standard for building web apps: and Apache Struts (along with J2EE framework) Standard patterns implemented in and Struts have helped reduce code complexity and accelerate development times
History In the early days of the Web, most business Web sites were simply a form of advertisement The dot-com revolution showed businesses that they could also provide online services for their customers. Early e-commerce Web sites were often powered by Java-based technologies, including JSPs, EJBs, Servlets, and JDBC; or Microsoft-based technologies, including ASP, VBScript, MTS, ADO, COM and COM+.
History In the early days of the Web, most business Web sites were simply a form of advertisement The dot-com revolution showed businesses that they could also provide online services for their customers. Early e-commerce Web sites were often powered by Java-based technologies, including JSPs, EJBs, Servlets, and JDBC; or Microsoft-based technologies, including ASP, VBScript, MTS, ADO, COM and COM+.
History In the early days of the Web, most business Web sites were simply a form of advertisement The dot-com revolution showed businesses that they could also provide online services for their customers. Early e-commerce Web sites were often powered by Java-based technologies, including JSPs, EJBs, Servlets, and JDBC; or Microsoft-based technologies, including ASP, VBScript, MTS, ADO, COM and COM+.
J2EE standard In the spring of 1999, Sun released the J2EE standard Although the front end was well architected and scalable, the system still broke down because the back-end business logic The need for more integrated business tools and Web services became apparent. This background laid the framework for the pattern revolution in J2EE.
J2EE standard In the spring of 1999, Sun released the J2EE standard Although the front end was well architected and scalable, the system still broke down because the back-end business logic The need for more integrated business tools and Web services became apparent. This background laid the framework for the pattern revolution in J2EE.
J2EE standard In the spring of 1999, Sun released the J2EE standard Although the front end was well architected and scalable, the system still broke down because the back-end business logic The need for more integrated business tools and Web services became apparent. This background laid the framework for the pattern revolution in J2EE.
J2EE standard In the spring of 1999, Sun released the J2EE standard Although the front end was well architected and scalable, the system still broke down because the back-end business logic The need for more integrated business tools and Web services became apparent. This background laid the framework for the pattern revolution in J2EE.
Model view controller Denition 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 input and presentation (UI), permitting independent development, testing and maintenance of each.
Graphic presentation
Model Denition The model is used to manage information and notify observers when that information changes. The model is the domain-specic representation of the data upon which the application operates. Domain logic adds meaning to raw data (for example, calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items). When a model changes its state, it noties its associated views so they can be refreshed.
View Denition The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for dierent purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.
Controller Denition The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.
Approach 1
Approach 2
Case example - Web Server The Model is the module which interacts with the database using for example SQL The View is the module which generates HTML from the data The Controller contains all the other functionality of the server (eg. it decides which page to display, generates dynamic content, handles session and cookie data, etc.)
Case example - Web Server The Model is the module which interacts with the database using for example SQL The View is the module which generates HTML from the data The Controller contains all the other functionality of the server (eg. it decides which page to display, generates dynamic content, handles session and cookie data, etc.)
Case example - Web Server The Model is the module which interacts with the database using for example SQL The View is the module which generates HTML from the data The Controller contains all the other functionality of the server (eg. it decides which page to display, generates dynamic content, handles session and cookie data, etc.)
Motivation Controller - View separation. Controller is completely unaware of the actual output format being used. Various views: html, pdf, ash... Controller - Model separation. Data is stored can be easily changed without having to modify the Controller or the View. Dierent data storage: raw le, database, xml..
Motivation Controller - View separation. Controller is completely unaware of the actual output format being used. Various views: html, pdf, ash... Controller - Model separation. Data is stored can be easily changed without having to modify the Controller or the View. Dierent data storage: raw le, database, xml..
Outline 1 2 3 4 5 6
MVC in implements MVC using the Page Controller pattern. The Page Controller pattern applies the controller at the level of individual pages. The runtime intercepts page requests, invokes the requested actions on the model, and determines the correct view to use for the resulting page. The interception and dispatching logic is automated and hidden from developers divides each page into two parts: a View that contains the various controls, and the code-behind
MVC in implements MVC using the Page Controller pattern. The Page Controller pattern applies the controller at the level of individual pages. The runtime intercepts page requests, invokes the requested actions on the model, and determines the correct view to use for the resulting page. The interception and dispatching logic is automated and hidden from developers divides each page into two parts: a View that contains the various controls, and the code-behind
MVC in implements MVC using the Page Controller pattern. The Page Controller pattern applies the controller at the level of individual pages. The runtime intercepts page requests, invokes the requested actions on the model, and determines the correct view to use for the resulting page. The interception and dispatching logic is automated and hidden from developers divides each page into two parts: a View that contains the various controls, and the code-behind
MVC in implements MVC using the Page Controller pattern. The Page Controller pattern applies the controller at the level of individual pages. The runtime intercepts page requests, invokes the requested actions on the model, and determines the correct view to use for the resulting page. The interception and dispatching logic is automated and hidden from developers divides each page into two parts: a View that contains the various controls, and the code-behind
MVC in implements MVC using the Page Controller pattern. The Page Controller pattern applies the controller at the level of individual pages. The runtime intercepts page requests, invokes the requested actions on the model, and determines the correct view to use for the resulting page. The interception and dispatching logic is automated and hidden from developers divides each page into two parts: a View that contains the various controls, and the code-behind
scheme
Eliminate code duplication with BaseController
Scenario using asp.net page controller
Benets using page controller pattern 1 Simplicity 2 Built-in framework features 3 Increased reuse 4 Expandability 5 Separation of developer responsibilities
Benets using page controller pattern 1 Simplicity 2 Built-in framework features 3 Increased reuse 4 Expandability 5 Separation of developer responsibilities
Benets using page controller pattern 1 Simplicity 2 Built-in framework features 3 Increased reuse 4 Expandability 5 Separation of developer responsibilities
Benets using page controller pattern 1 Simplicity 2 Built-in framework features 3 Increased reuse 4 Expandability 5 Separation of developer responsibilities
Benets using page controller pattern 1 Simplicity 2 Built-in framework features 3 Increased reuse 4 Expandability 5 Separation of developer responsibilities
Liabilities of using page controller pattern 1 One controller per page 2 Deep inheritance trees 3 Dependency on the Web framework
Liabilities of using page controller pattern 1 One controller per page 2 Deep inheritance trees 3 Dependency on the Web framework
Liabilities of using page controller pattern 1 One controller per page 2 Deep inheritance trees 3 Dependency on the Web framework
Outline 1 2 3 4 5 6
MVC in Struts The Struts architecture is essentially derived from a combination of the Front Controller and Intercepting Filter patterns Single controller that governs the application events Filters, for example, the Struts Validator is a lter that ensures that the controller receives only validated requests The controller itself is usually implemented in two parts: a handler and a hierarchy of commands (Gamma, 1995)
MVC in Struts The Struts architecture is essentially derived from a combination of the Front Controller and Intercepting Filter patterns Single controller that governs the application events Filters, for example, the Struts Validator is a lter that ensures that the controller receives only validated requests The controller itself is usually implemented in two parts: a handler and a hierarchy of commands (Gamma, 1995)
MVC in Struts The Struts architecture is essentially derived from a combination of the Front Controller and Intercepting Filter patterns Single controller that governs the application events Filters, for example, the Struts Validator is a lter that ensures that the controller receives only validated requests The controller itself is usually implemented in two parts: a handler and a hierarchy of commands (Gamma, 1995)
MVC in Struts The Struts architecture is essentially derived from a combination of the Front Controller and Intercepting Filter patterns Single controller that governs the application events Filters, for example, the Struts Validator is a lter that ensures that the controller receives only validated requests The controller itself is usually implemented in two parts: a handler and a hierarchy of commands (Gamma, 1995)
scheme
- typical scenario
Scenario using struts front controller
Benets using front controller pattern 1 Centralized control 2 Thread-safety 3 Congurability
Benets using front controller pattern 1 Centralized control 2 Thread-safety 3 Congurability
Benets using front controller pattern 1 Centralized control 2 Thread-safety 3 Congurability
Liabilities of using front controller pattern 1 Performance considerations 2 Increased complexity
Liabilities of using front controller pattern 1 Performance considerations 2 Increased complexity
Outline 1 2 3 4 5 6
Overview 1 Follows MVC architecture 2 Utilizes a centralized servlet controller 3 Multiple business logic adapters, JSP page 4 structs-cong.xml conguration le
Overview 1 Follows MVC architecture 2 Utilizes a centralized servlet controller 3 Multiple business logic adapters, JSP page 4 structs-cong.xml conguration le
Overview 1 Follows MVC architecture 2 Utilizes a centralized servlet controller 3 Multiple business logic adapters, JSP page 4 structs-cong.xml conguration le
Overview 1 Follows MVC architecture 2 Utilizes a centralized servlet controller 3 Multiple business logic adapters, JSP page 4 structs-cong.xml conguration le
Overview 1 Follows MVC architecture 2 Utilizes a centralized servlet controller 3 Multiple business logic adapters, JSP page 4 structs-cong.xml conguration le
M - V - C The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data. Mainly implemented with JavaBeans or EJB components The view is responsible for dispalying the results back to the user. Tech: JSP, Struts tags library, Tile framework, XLST etc. ActionServlet, Action classes (Command design pattern implementation of the Java Servlet technology).
M - V - C The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data. Mainly implemented with JavaBeans or EJB components The view is responsible for dispalying the results back to the user. Tech: JSP, Struts tags library, Tile framework, XLST etc. ActionServlet, Action classes (Command design pattern implementation of the Java Servlet technology).
M - V - C The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data. Mainly implemented with JavaBeans or EJB components The view is responsible for dispalying the results back to the user. Tech: JSP, Struts tags library, Tile framework, XLST etc. ActionServlet, Action classes (Command design pattern implementation of the Java Servlet technology).
Workow
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
Workow 1 The ActionServlet receives the request. 2 Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 3 Decides which action class to invoke to process the request. 4 Validate the data entered by the user. 5 After completing the request processing the Action class returns an ActionForward to the controller. 6 The action class process the request with the help of the model component. The model interacts with the database and process the request. 7 Based on the ActionForward the controller will invoke the appropriate view. 8 The HTTP response is rendered back to the user by the view component.
struts-cong.xml Contains the details regarding the Actions, ActionForms, ActionMappings and ActionForwards. During the startup the ActionServelet reads the struts-cong.xml le and creates a database of conguration objects. Later while processing the request the ActionServlet makes decision by refering to this object.
struts-cong.xml
Struts 1.x components
Example application
Example application
Example application
Example application
Example application
Example application
Example application
Example application
Example application
Example application
Example application
Outline 1 2 3 4 5 6
Example application
Example application
Example application
Example application
Example application
Outline 1 2 3 4 5 6
Page vs
Page vs
J2EE vs As Web applications mature, the need for a robust, fully-integrated development system increases Each framework oers a wide range of features and benets for application development. On a development level, developing with involves less code than Struts Many features, such as validation, caching and tracing, are built into, whereas Struts requires third-party components
J2EE vs As Web applications mature, the need for a robust, fully-integrated development system increases Each framework oers a wide range of features and benets for application development. On a development level, developing with involves less code than Struts Many features, such as validation, caching and tracing, are built into, whereas Struts requires third-party components
J2EE vs As Web applications mature, the need for a robust, fully-integrated development system increases Each framework oers a wide range of features and benets for application development. On a development level, developing with involves less code than Struts Many features, such as validation, caching and tracing, are built into, whereas Struts requires third-party components
J2EE vs As Web applications mature, the need for a robust, fully-integrated development system increases Each framework oers a wide range of features and benets for application development. On a development level, developing with involves less code than Struts Many features, such as validation, caching and tracing, are built into, whereas Struts requires third-party components
J2EE vs What are the real advantages of using J2EE over ASP.net? Microsoft sometimes make random changes to their technology direction, and you have no recourse because you are locked into a single-vendor solution Is there anything that cannot be done in ASP.net that can be done in J2EE? In J2EE, standards compliance means you can re any vendor and replace them with a better, more responsive vendor. Also, once the frameworks are all in place and congured, is it faster to develop apps in J2EE than it is in.net? No.
J2EE vs What are the real advantages of using J2EE over ASP.net? Microsoft sometimes make random changes to their technology direction, and you have no recourse because you are locked into a single-vendor solution Is there anything that cannot be done in ASP.net that can be done in J2EE? In J2EE, standards compliance means you can re any vendor and replace them with a better, more responsive vendor. Also, once the frameworks are all in place and congured, is it faster to develop apps in J2EE than it is in.net? No.
J2EE vs What are the real advantages of using J2EE over ASP.net? Microsoft sometimes make random changes to their technology direction, and you have no recourse because you are locked into a single-vendor solution Is there anything that cannot be done in ASP.net that can be done in J2EE? In J2EE, standards compliance means you can re any vendor and replace them with a better, more responsive vendor. Also, once the frameworks are all in place and congured, is it faster to develop apps in J2EE than it is in.net? No.
J2EE vs What are the real advantages of using J2EE over ASP.net? Microsoft sometimes make random changes to their technology direction, and you have no recourse because you are locked into a single-vendor solution Is there anything that cannot be done in ASP.net that can be done in J2EE? In J2EE, standards compliance means you can re any vendor and replace them with a better, more responsive vendor. Also, once the frameworks are all in place and congured, is it faster to develop apps in J2EE than it is in.net? No.
Motivation Web programming is part of a Software Engineering course on Faculty of Mathematics in Belgrade In the past few years page controller concept through asp.net was preferable In general.net technologies were used in this course Students don't learn java based web programming through this or any other course
Motivation Web programming is part of a Software Engineering course on Faculty of Mathematics in Belgrade In the past few years page controller concept through asp.net was preferable In general.net technologies were used in this course Students don't learn java based web programming through this or any other course
Motivation Web programming is part of a Software Engineering course on Faculty of Mathematics in Belgrade In the past few years page controller concept through asp.net was preferable In general.net technologies were used in this course Students don't learn java based web programming through this or any other course
Motivation Web programming is part of a Software Engineering course on Faculty of Mathematics in Belgrade In the past few years page controller concept through asp.net was preferable In general.net technologies were used in this course Students don't learn java based web programming through this or any other course
Final conclusion Technology shouldn't be discussed. It is much like a matter of taste Here, the web programming concept is questioned. Too bad is that java vendors focused on front controller, and Microsoft on page controller pattern In the past few years MS made its own MVC framework Both concept should be a part of course, so question arise: Java based MVC framework or still developing asp.net MVC framework?
Final conclusion Technology shouldn't be discussed. It is much like a matter of taste Here, the web programming concept is questioned. Too bad is that java vendors focused on front controller, and Microsoft on page controller pattern In the past few years MS made its own MVC framework Both concept should be a part of course, so question arise: Java based MVC framework or still developing asp.net MVC framework?
Final conclusion Technology shouldn't be discussed. It is much like a matter of taste Here, the web programming concept is questioned. Too bad is that java vendors focused on front controller, and Microsoft on page controller pattern In the past few years MS made its own MVC framework Both concept should be a part of course, so question arise: Java based MVC framework or still developing asp.net MVC framework?
Final conclusion Technology shouldn't be discussed. It is much like a matter of taste Here, the web programming concept is questioned. Too bad is that java vendors focused on front controller, and Microsoft on page controller pattern In the past few years MS made its own MVC framework Both concept should be a part of course, so question arise: Java based MVC framework or still developing asp.net MVC framework?
Thanks for listening!