Download presentation
Presentation is loading. Please wait.
Published byLewis Gardner Modified over 8 years ago
1
Model View Controller
2
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.
3
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.
4
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?
5
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?
6
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?
7
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.
8
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
9
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.
10
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.
11
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
13
Struts2 is MVC Based Framework
14
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
15
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Hello World Hello World From Struts2 Please enter your name View
16
Hello World Hello World, View
17
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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
18
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> /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
19
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
20
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
21
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
22
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
23
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" method="execute"> <exception-mapping exception="java.lang.NullPointerException" result="error" /> /HelloWorld.jsp /Error.jsp Exception Handling
24
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" "http://struts.apache.org/dtds/struts-2.0.dtd"> <exception-mapping exception="java.lang.NullPointerException" result="error" /> <action name="hello" class="com.helloworld.HelloWorldAction" method="execute"> /HelloWorld.jsp /Error.jsp Global Exception Handling
25
Struts2 Framework provides built in JSP Tags(just like JSTL), which can be used in JSP files Struts2 Form Tags
26
to get property of bean Struts2 Form Tags
27
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
28
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
29
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
30
Action and other objects are pushed into the Object Stack where as maps (parameters, request, session, application, attr) are stored in Context Map.
31
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.