Model View Controller. MVC is the separation of model, view and controller. It's simply a paradigm; an ideal that you should have in the back of your.

Slides:



Advertisements
Similar presentations
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Advertisements

Apache Struts Technology
Web MVC-2: Apache Struts Rimon Mikhaiel
DT211/3 Internet Application Development JSP: Processing User input.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
Internet Technologies 1 Master of Information System Management Java Server Faces Model/View/Controller Design Pattern for Web Development Slides.
JavaServer Faces. Objectives To implement dynamic web pages with JavaServer Faces (JSF) technology To learn the syntactical elements of JavaServer Faces.
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
DT228/3 Web Development JSP: Directives and Scripting elements.
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
Apache Struts Technology A MVC Framework for Java Web Applications.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Session-01. Layers Struts 2 Framework The struts 2 framework is used to develop MVC-based web application. Struts 1.0 was released in June The.
Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Stateful Web Application Development with Spring Web Flow John Case Senior Consultant Centare Group, Ltd.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
Struts 2.0 an Overview ( )
Introduction to Struts 2.0 Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv Software School,Fudan University 1.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
WebWork in Action An introduction to WebWork Patrick A. Lightbody.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
ABSTRACT Zirous Inc. is a growing company and they need a new way to track who their employees working on various different projects. To solve the issue.
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
Welcome to Library Management System
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
JSF Framework Java Server Faces Presented by Songkran Totiya (6/10/2014)
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the.
REVOLUTION TO NEXT GENERATION Struts2.0 20/02/ Avishek Arang ::
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
1 Copyright © 2004, Oracle. All rights reserved. Oracle Application Development Framework.
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 9 Web Application Design. Objectives Describe the MVC design pattern as used with Web applications Explain the role and responsibilities of each.
Struts 2 Introduction.
Data transfer and type conversion One of the common tasks of the web application domain was moving and converting data from string-based HTTP to the various.
Chapter 4 Request and Response. Servlets are controlled by the container.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Chapter 2 Java Struct 2. Content Basic MVC Architecture Struts 2 Overview Struts 2 - Architecture.
SDJ INFOSOFT PVT. LTD. 2 BROWSERBROWSER JSP JavaBean DB Req Res Application Layer Enterprise server/Data Sources.
Apache Struts Technology A MVC Framework for Java Web Applications.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Knowledge Byte In this section, you will learn about:
The Model Layer What is Model?
Presentation transcript:

Model View Controller

MVC is the separation of model, view and controller. It's simply a paradigm; an ideal that you should have in the back of your mind when designing classes. Avoid mixing code from the three categories into one class.

Model. The model represents data and where business logic is performed, which may cause changes to data, like retrieving data from database, etc… View. The view renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed. Controller. The controller translates the user's interactions with the view into actions that the model will perform.

The view is the interface the user sees and interacts with. For Web applications, this has historically been an HTML(with CSS and JavaScript) interface.HTML Handling all of these interfaces in your application is becoming increasingly challenging. Advantage of MVC Design Pattern A big advantage of MVC is that it handles the use of many different views for your application. Common Model and Controllers can be used, for different views(like Desktop, Mobile App based, etc…) There’s no real processing happening in the view; it serves only as a way to output data and allow the user to act on that data, whether it is an online store or an employee list. What is View?

The next component of MVC, the model, represents enterprise data and business rules. It's where most of the processing takes place when using MVC. Databases fall under the model. The data returned by the model is display- neutral, meaning that the model applies no formatting. This way, a single model can provide data for any number of display interfaces. This reduces code duplication, because model code is only written once and is then reused by all of the views. What is Model?

Finally, the controller interprets requests from the user and calls portions of the model and view as necessary to fulfill the request. So when the user clicks a Web link or submits an HTML form, the controller itself doesn’t output anything or perform any real processing. It takes the request and determines which model components to invoke and which formatting to apply to the resulting data. What is Controller?

to summarize, a user request is interpreted by the controller, which determines what portions of the model and view to call. The model handles interaction with data and applies business rules and then returns data. Finally, the appropriate view is determined and formatting is applied to the resulting data for presentation. And then View gets displayed to user.

First, and possibly most important, is the ability to have multiple views that rely upon a single model. As I mentioned, there's an increasing demand on new ways to access your application. A solution to this is to use MVC. With MVC, it doesn’t matter if the user wants a Flash interface or a WAP one; the same model can handle either. Code duplication is limited because you’ve separated the data and business logic from the display.WAP

Because the model returns data without applying any formatting, the same components can be used and called for use with any interface. For example, most data might be formatted with HTML, but it could also be formatted with Macromedia Flash or WAP. The model also isolates and handles state management and data persistence.

Because the model is self-contained and separate from the controller and the view, it's much less painful to change your data layer or business rules. If you switch databases, say from MySQL to Oracle, or change a data source from an RDBMS to LDAP, you need only alter your model. If written correctly, the view won’t care at all whether that list of users came from a database or an LDAP server. The three parts of an MVC application are black boxes whose inner workings are hidden from the other portions. It makes you build well-defined interfaces and self- contained components.

Another advantage of MVC is, since developers are of different skill sets, web developers can focus only on View part, and actual developers can focus on Model and Controller

Struts2 is MVC Based Framework

In Struts2, Action class is the Model. Below is snapshot of code in Action class. execute() is compulsory in Action class. If some other name is used instead of execute, that need to be configured in struts.xml config file. public class HelloWorldAction extends ActionSupport{ private String name; public String execute() throws Exception { return "success"; //if successful return “error”; //if error //the views which need to be displayed in success or error //conditions, can be set in struts.xml } public String getName() { return name; } public void setName(String name) { this.name = name; } Action class

page language="java" contentType="text/html; charset=ISO " pageEncoding="ISO "%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " Hello World Hello World From Struts2 Please enter your name View

Hello World Hello World, View

<web-app xmlns:xsi=" xmlns=" xmlns:web=" xsi:schemaLocation=" id="WebApp_ID" version="3.0"> Struts 2 index.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* web.xml (Deployment Descriptor) This is compulsory for Struts to work. After configuring this, when user sends request from HTML, control goes to Struts directly, Struts further invokes corresponding Action

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " /index.jsp <action name="hello" class="com.helloworld.HelloWorldAction" method="execute"> /HelloWorld.jsp Struts.xml (Struts Configuration file) Details of a single Action Name of Action class Mapping List of next views to be displayed

1.User sends a request to the server for requesting for some resource (i.e. pages). 2.The Filter Dispatcher looks at the request and then determines the appropriate Action. 3.Configured interceptor functionalities applies such as validation, file upload etc. 4.Selected action is performed based on the requested operation. 5.Again, configured interceptors are applied to do any post-processing if required. 6.Finally, the result is prepared by the view and returns the result to the user. Steps involved with Struts2 Framework

If any validations of inputs data need to be performed, then validate() method need to be provided in corresponding Action class. Struts 2 will automatically execute the validate method and if any of the “if” statements listed inside the validate method are true, Struts 2 will call its addFieldError method. If any errors have been added, then Struts 2 will not proceed to call the execute method. Rather the Struts 2 framework will return input as the result of calling the action. Below line need to be added to validate() method of corresponding Action class. addFieldError("name“ /*form field*/,"The name is required"); we need to add the following result to our action node in struts.xml. /index.jsp Also, below interceptor need to be added to struts.xml, for validate() method in Action class, to get invoked by Struts Framework Validation

Interceptors are conceptually the same as servlet filters. Interceptors perform below Providing preprocessing logic before the action is called. Providing postprocessing logic after the action is called. Below need to be added to struts.xml, eg. : The params interceptor helps in transferring the request data onto the action object. : Shows simple profiling information in the form of how long the action takes to execute, in the logs. Interceptors

Struts provides an easier way to handle uncaught exception and redirect users to a dedicated error page. You can easily configure Struts to have different error pages for different exceptions. Struts makes the exception handling easy by the use of the "exception" interceptor. The "exception" interceptor is included as part of the default stack, so you don't have to do anything extra to configure it. It is available out-of-the-box ready for you to use. Exception Handling

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" method="execute"> <exception-mapping exception="java.lang.NullPointerException" result="error" /> /HelloWorld.jsp /Error.jsp Exception Handling

We have seen how we can handle action specific exception. We can set an exception globally which will apply to all the actions. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " <exception-mapping exception="java.lang.NullPointerException" result="error" /> <action name="hello" class="com.helloworld.HelloWorldAction" method="execute"> /HelloWorld.jsp /Error.jsp Global Exception Handling

Struts2 Framework provides built in JSP Tags(just like JSTL), which can be used in JSP files Struts2 Form Tags

to get property of bean Struts2 Form Tags

Struts2 Framework supports Internationalization or Localization. Internationalization (i18n) is the process of planning and implementing products and services so that they can easily be adapted to specific local languages and cultures, a process called localization. The internationalization process is called translation or localization enablement. Internationalization is abbreviated i18n because the word starts with the letter “i” and ends with “n”, and there are 18 characters between the first i and the last n. Internationalization/Localization

OGNL(Object Graph navigation Language) is an expression that can set and get the property of java object. In struts 2 OGNL is used to associate Action class and UI components. OGNL uses a standard naming convention to evaluate the Expression. OGNL is the root object for the value stack. OGNL expression can set and get values from application, session, value stack, action, request, parameters, and attr. Value stack is the root of OGNL. Action class resides in value stack so we directly fetch action class items using OGNL expression as Value Stack or OGNL

But in case of session or request we need to use # as below. Use of OGNL OGNL is used in struts to access model objects from a jsp page. It can be used to bind GUI elements to model objects in struts framework. It can create lists,maps to be used with GUI. Value Stack or OGNL

Action and other objects are pushed into the Object Stack where as maps (parameters, request, session, application, attr) are stored in Context Map.

Value Stack or OGNL Maps in the Context Map parameters. Is a map containing the parameters in the current request. request. Is a map containing attributes in the current request. session. Is a map containing the session attributes for the current user. application. Is a map containing the ServletContext attributes. attr. Is a map that searches for attributes in the the order: request, session, application. OGNL can be used to access objects in the Object Stack and the Context Map. To access Context Map properties we use a # in front of the OGNL expression, if not used search will take place from Object Stack.