Introduction to MVC Mode

Source: Internet
Author: User
Tags ldap tld

What is MVC?

MVC is short for three words: model, view, and controller ). The purpose of the MVC model is to achieve the functional division of the web system. The model layer implements the business logic in the system, which can usually be implemented using a JavaBean or EJB. The view layer is used for interaction with users. It is usually implemented using JSP. The Controller layer serves as a bridge between model and view. It can dispatch user requests and select an appropriate view for display, it can also interpret user input and map them to operations that can be performed at the model layer.

MVC and template concepts

Model View Controller (MVC) Model-View-Controller)

MVC originally exists in mongot

In the op program, M is the exponential data model, V is the user interface, and C is the controller. Use MVC

The purpose is to separate the implementation code of M and V so that the same program can use different forms of representation. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure synchronization between m and V. Once M changes, V should be updated synchronously.

Model-View-controller (MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. It has been widely used. In recent years, it has been recommended to be the design model of Oracle's Sun Company Java EE platform, and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.

How MVC works

MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: model, view, and controller. They process their own tasks.

View

A view is the interface on which the user sees and interacts with it. For older Web applications, a view is an interface composed of HTML elements. In New web applications, HTML still plays an important role in the view, however, some new technologies have emerged, including Macromedia Flash and some identification languages such as XHTML, XML/XSL, and WML and Web services.

How to process the application interface becomes increasingly challenging. One major benefit of MVC is that it can process many different views for your applications. In fact, there is no real processing in the view. Whether the data is stored online or an employee list, as a view, it is just a way to output data and allow users to manipulate it.

Model

The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as ejbs and ColdFusion components to process databases. The data returned by the model is neutral, that is, the model has nothing to do with the data format, so that a model can provide data for multiple views. Because the Code applied to the model can be reused by multiple views only once, code duplication is reduced.

Controller

The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on a web page and send an HTML form, the controller does not output anything or process anything. It only receives the request and determines which model component is called to process the request, and then determines which view is used to display the returned data.

Why use MVC?

Most Web applications are created using a procedural language like ASP, PHP, or CFML (the PHP version has fully supported the object-oriented model. They mix data-Layer Code such as database query statements and presentation-Layer Code such as HTML. Experienced developers will separate the data from the presentation layer, but this is usually not easy to do. It requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings us are uncertain.

First, the most important thing is that multiple views can share a model. Now you need to access your applications in more and more ways. In this regard, one solution is to use MVC, whether your users want the flash interface or WAP interface, they can be processed using a model. Since you have separated Data from business rules from the presentation layer, You can reuse your code to the maximum extent.

Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be represented in HTML, but they may also be represented in Adobe Flash and WAP. The model also provides State management and data persistence processing functions. For example, session-based shopping carts and e-commerce processes can also be reused by flash websites or wireless networking applications.

Because the model is self-contained and separated from the Controller and view, it is easy to change the data layer and business rules of your application. If you want to port your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, you only need to change your model. Once you implement the model correctly, the view will display the data correctly regardless of whether the data comes from the database or the LDAP server. Because the three components of an application using MVC are independent of each other, changing one of them will not affect the other two, you can build a loose coupling component based on this design idea.

For me, the Controller also provides the advantage of using the Controller to connect different models and views to meet user needs. In this way, the controller can provide a powerful means to construct applications. Given some reusable models and views, the controller can select a model for processing based on the user's needs, and then select a view to display the processing results to the user.

Advantages of MVC

Low Coupling

The view layer and business layer are separated so that you can modify the view layer code without re-compiling the model and controller code. Similarly, to change the business flow or rules of an application, you only need to change the MVC model layer. Because the model is separated from the Controller and view, it is easy to change the data layer and business rules of the application.

High reusability and Applicability

As technology advances, more and more methods are needed to access applications. The MVC mode allows you to access the code on the same server using different views. It includes any web (HTTP) browser or wireless browser (WAP). For example, you can use a computer or mobile phone to order a product. Although the ordering method is different, however, the method for handling product subscriptions is the same. Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be expressed in HTML, but may also be expressed in WAP. The commands required to represent these data are to change the implementation method of the view layer, the control layer and model layer do not need to be changed.

Low Cost of Life Cycle

MVC makes it possible to reduce the technical content of developing and maintaining user interfaces.

Rapid deployment

Using the MVC pattern, the development time is greatly reduced. It enables programmers (Java developers) to focus on business logic and interface programmers (HTML and JSP developers) focus on the form of expression.

Maintainability

Separating the view layer and business logic layer also makes Web applications easier to maintain and modify.

Conducive to Software Engineering Management

Because different layers perform their jobs, different applications have some same features, which is conducive to managing program code through engineering and tools.

Disadvantages of MVC

The disadvantage of MVC is that it is not easy to fully understand MVC because it is not clearly defined. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it.

You will have to spend a considerable amount of time thinking about how to apply MVC to your application, and because the model and view must be strictly separated, this also makes it difficult to debug the application. Each component must be thoroughly tested before use. Once your components have been tested, you can reuse them without any scruples.

Based on the developer's experience, because the developer divides an application into three parts, using MVC also means that you are going to manage more files than before, which is obvious. It seems that our workload has increased, but remember that this is not worth mentioning compared to the benefits it can bring us.

MVC is not suitable for small or medium-sized applications. It is usually not worth the candle if you spend a lot of time applying MVC to a small or medium-sized application.

The MVC design pattern is a good way to create software. It advocates some principles, such as separation of content and display, which may be better understood. However, if you want to isolate the components of models, views, and controllers, you may need to rethink your applications, especially the application architecture. If you are willing to accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will put your software on a new level in terms of robustness, code reuse, and structure.

Development Method

Java Web Application Development

Java Web application development involves several development methods that comply with the MVC design pattern.

1: JSP + servlet + JavaBean (EJB)

2: JSP + JavaBean (Controller) + JavaBean (EJB) (model)

3: TDK (turbine, velocity ...)

4: xsp

5: JSP + Struts + JavaBean (EJB)

6: SSH (STRUTS + spring + hibernate)

. NET web application development

. NET web application development can adopt:

1: ASPnet MVC Framework (ASPnet MVC)

2: monorail (RC3)

3: ASPnet MVC2

PHP Web Application Development

PHP Web application development can adopt:

1. Official Zend framework PHP framework

2. fleaphp/qeephp and other popular domestic frameworks

3. Popular frameworks such as CakePHP

4. other frameworks such as thinkphp

Common MVC Components

Struts: Apache, the most popular MVC component

Struts2: a new product that combines struts and webwork with Apache. It is currently on the rise

Webwork: The old MVC component was later combined into struts2, but it is still developing.

Spring MVC: springframework integrates its own spring advantages to launch MVC components, and many users

JSF: This is a standard. Sun and Apache both have their own implementations. A large number of users are supported by many ides.

Tapestry: the most thorough MVC development framework, rich component resources, high reusability. A component acts as the controller and acts as a medium between the pure-domain objects in the model and HTML templates containing components. In most cases, this method is applied to pages (pages are also the tapestry component). However, in some cases, a component has its own template and contains more components, it also supports intercommunication with users. The page connects the mode layer and performance Layer by configuring a series of property expressions. Attribute expressions use another open-source framework, ognl (Object
Graph Navigation language ). The open-source project of ognl is independent of tapestry, but it plays a very important role in tapestry. Ognl mainly aims to read and update the Java Bean attributes of objects.

. Net MVC: the MVC component on. NET, after preview1 ~ 5. RC1 and RC2 are now in the official version. Microsoft defines the architecture that can be used for production. With vs2008 and vs2010, we believe that. Net MVC will be an important member of the MVC family.

Introduction to model 1 and Model 2 in struts

One of the concepts we often mention when developing Web applications is model1/model2. What does it mean? In fact, It is a description of the different models that use JSP technology to form web applications. Next we will give a brief introduction to this concept.

Model 1

In the use of Java technology to build Web applications, due to the development of JSP technology, this easy-to-Master and fast-developed technology has become the main technology for creating web applications. JSP pages can be easily displayed on JSP pages by combining the business logic (JSP: usebean), server processing process (JSP: Let), and HTML, business logic and process control allow you to quickly complete application development. Nowadays, many web applications are composed of a group of JSP pages. This JSP-centered development model is called model1.

Of course, this development mode has great advantages in fast and small-scale application development, but it also has some disadvantages from the engineering perspective:

The implementation of an application is generally based on a process. A group of JSP pages implement a business process. To modify the process, you must modify it in multiple places. This is not conducive to application expansion and updates.

Because the application is not built on a module, the business logic and presentation logic are not abstracted and separated in the JSP page. Therefore, it is not conducive to Application System Business reuse and modification.

Considering these problems, different design patterns must be used for developing large Web applications-this is model2.

Model 2

Model 2 represents a framework based on the MVC pattern. MVC is short for Model-View-controller. "Model" indicates the business logic of the application (implemented by the JavaBean and EJB components), and "View" indicates the application surface (generated by the JSP page ), "controller" is to provide application processing process control (generally a servlet). Through this design model, the application logic, processing process and display logic are divided into different components for implementation. These components can be used for interaction and reuse. This makes up for the shortcomings of model1.

Model2 has the advantages of componentization, which makes it easier to develop and manage large-scale systems. However, developing a strutsmvc system is much more complicated than simply developing a JSP system. It requires more time to learn and grasp it. At the same time, the introduction of new things will bring new problems (this reminds me of an article about "Automatic Computing", which mentions that in order to reduce the complexity of the system, it leads to a higher complexity ).

You must rethink and design the application structure based on the strutsmvc component. Applications that can be implemented by creating a simple JSP page are now designed and implemented in multiple steps.

All pages and components must be implemented in the struts MVC Framework, so additional development is required.

Strutsmvc itself is a very complex system, so when using strutsmvc to implement web applications, it is best to select a ready-made MVC framework for development, so as to get twice the result with half the effort. There are a lot of available MVC frameworks. Because struts has complete documentation and is relatively simple, it is easier to use it to develop the MVC system.

Struts structure and processing process

Struts1 is a project of the Apache organization. Like other projects of the Apache organization, it is also an open source project. Struts1 is a good MVC framework that provides underlying support for developing MVC systems. It uses servlet, JSP, and customtaglibrary technologies.

As a MVC Framework, struts1 provides implementation components for model, view, and controller, respectively, to see how they are combined.

Controller: the Controller accepts requests from the client, executes the corresponding business logic, and sends the response result back to the client. In struts1, the controller function consists of actionservlet and actionmapping objects in the figure. The core is a servlet-type object actionservlet, which is used to accept client requests. Actionservlet includes a set of configuration-based actionmapping objects. Each actionmapping object maps a request to an action processor object in a specific model.

Model: The model in the strutsmvc system can be divided into two types: the internal state of the system and the action of changing the state of the system. Struts1 provides action and actionform objects for model: All action processor objects are subclasses derived from the action class of struts1. The action processor object encapsulates the specific processing logic, calls the business logic module, and submits the response to the appropriate view component to generate a response. The actionform component object provided by struts1. It can describe the client form data by defining attributes. Developers can derive subclass objects from them and use them in combination with the custom tag library provided by struts to encapsulate and support the form data on the client, an action processor object can be read and written directly without data interaction with the request and response objects. The actionform Component Object supports interaction between views and models. Struts1 generally recommends that you use a set of Javabean to indicate the internal status of the system. You can also use components such as entityejb and sessionejb to implement the system status based on the complexity of the system. Struts recommends separating "what" (Action) from "how to do" (business logic) during implementation. In this way, business logic can be reused.

View: View in the struts1 application is implemented by JSP technology. Struts1 provides a custom tag library that can be used to interact with the system's model, by using the JSP forms created by these custom tags, You can map them to the actionform in the model section to encapsulate user data, these custom tags also provide various display functions such as template customization.

The processing process of the strutsmvc framework clearly reflects the characteristics of the MVC System and the simple struts component structure. Strutscontrolleractionservlet processes customer requests and maps requests to the action processor object using the configured actionmapping object for processing. Action processes the data of an object accessing actionform, processes and responds to customer requests. It also calls the Bean components in the background, which encapsulate the specific business logic. The action processor notifies the controller of the processing result and the Controller proceeds with the next step.

Development Using Struts Framework

What the struts1 MVC system should do

Because struts has provided us with a very good MVC framework, we can greatly speed up development when developing the MVC system using struts. A development process that can be used during development is as follows (Reference 3 ):

Collect and define application requirements.

Defines and develops "screen display" requirements based on the principles of data collection and display.

Define the access path for each "screen display.

Define the relationship between actionmappings and application business logic.

Develop all supporting objects that meet the "screen display" requirement.

Create an actionform object based on the data attributes provided by each "screen display" requirement

Develop the action object called by actionmapping.

Develop application business logic objects (beans, ejbs, and so on ).

Create a JSP page for the actionmapping design process.

Create an appropriate profile struts-config.xml, Web. xml.

Development/test/deployment

When using the Struts framework, the development of each part mainly includes:

Model: design and implement the business logic of the system using the JavaBean and EJB components. The specific action processing object is derived from the action according to different requests. Complete the "what to do" task to call the service components composed of beans. Create a derived class of actionform to encapsulate the form data of the client.

Controller part: Struts provides us with the implementation of the core control part. We only need to configure the actionmapping object

View part: to use the actionform object in the model, we must use the custom tag provided by struts to create an HTML form. Use the custom tag library provided by struts to compile the user interface to separate the application logic from the display logic. The Struts framework uses these custom tags to establish the relationship between views and models. The custom tag of Struts also provides many custom page functions.

You also need to edit two configuration files: Web. xml and struts-config.xml. Configure the interaction between modules in the struts system. The following describes the two configuration files:

Configuration of the web. xml file:

Web. XML in Web applications is the first place to be configured. It describes the Controller object of the system. Add the following tag to Web. xml:

<Servlet> <servlet-Name> action </servlet-Name> <servlet-class>

Org. Apache. Struts. Action. actionservlet </servlet-class> <init-M>

<M-Name> application </m-Name> </servlet>

Note: this servlet object is the Controller provided by struts. You can also specify initialization parameters for it, such as support for system application attributes.

<SERVLET-MAPPING> <SERVLET-NAME> action </SERVLET-NAME> <URL-PATTERN> *. DO </URL-PATTERN> </SERVLET-MAPPING>

Description: ing between the URL Information of the customer request and the specific processing on the server side.

<Taglib> <taglib-URL>/WEB-INF/struts-bean.tld </taglib-URL>

<Taglib-location>/WEB-INF/struts-bean.tld </taglib-location> </taglib>

Note: Add a reference to the custom tag library used by the application provided by struts.

Struts-config.xml file Configuration:

Struts-config.xml is used to establish the relationship between the Controller and the model. It describes the request corresponding to the specific processing rule used by the Controller, and also describes the ing between the data provided by the customer and the actionform component.

Add the following tag in the struts-config.xml

<Form-beans> <form-beanname = "loginform" type = "loginform"/> </form-beans>

Description: a tag describes a specific actionform subclass object. It can be used with custom tags on the JSP page to realize data ing between actionform and view.

<Action-mappings> <actionpath = "/login" type = "loginaction"

Name = "loginform" input = "/login. jsp"/> </Action-mappings>

Description: The tag describes the one-to-one ing between requests and processes. The input and path attributes uniquely indicate a request from the client. The name attribute describes the actionform subclass object that encapsulates the data of the client. The Type attribute describes the action subclass object that processes the request.

Through the configuration of the two configuration files, the MVC part of the struts MVC framework is linked to implement a real struts MVC system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.