Design of Web Application Framework Based on Model-View-Controller

Publisher:知识智慧Latest update time:2011-05-30 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Traditional web applications contain code to manage user interactions in the presentation layer, for example, a page can determine the next page the user will jump to. Developers often write all interaction code in the user interface code. However, after developers write page navigation code into the page code, the code becomes complex and difficult to reuse, maintain, and extend. For the entire application, its business logic, control logic, and running state are difficult to reuse. In many cases, the running state of the application needs to be maintained. But if the state is saved in a certain page, the code must retrieve the state from this page. The code written in this way is not elegant enough and it is difficult to get the expected effect. Moreover, these inelegant codes affect the extension and reuse of web pages. Based on the MVC pattern, this paper proposes an extensible framework that simplifies the processing of business logic code separated from web pages, which is conducive to the reuse of complex web page navigation and workflow processing.


1 MVC design pattern
MVC was proposed by Trygve Reenskaug and was first applied in the SmallTalk-80 environment. It is the basis for many interactive and interface systems. The MVC structure is designed for applications that need to provide multiple views for the same data. It well implements the separation of the data layer and the presentation layer. As a development model, MVC is usually used in the design and analysis of distributed application systems; in addition, it can also be used to determine the organizational relationship between the various parts of the system. In response to the need for variability in interface design, MVC decomposes the components of the interactive system into three components: view, controller, and model.
The view component displays information representing model data, logical relationships, and status to the user in a specific form. It obtains display information from the model, and can have multiple different display forms or views for the same information. The controller component is mainly used to handle the interaction between the user and the software. Its responsibility is to control the propagation of any changes in the model and ensure the corresponding connection between the user interface and the model; it accepts user input, feeds the input back to the model, and then realizes the calculation control of the model. It is the component that makes the model and view work in coordination. The model component stores data displayed by the view and controlled by the controller; it encapsulates the core data of the problem and the calculation relationship of logic and function, and is independent of the specific interface expression and I/O operation.
The separation of model, view and controller allows a model to have multiple display views. If the user changes the data of the model through the controller of a certain view, all other views that depend on this data should reflect these changes. Therefore, whenever any data change occurs, the controller will notify all views of the change, resulting in an update of the display. This is actually a change-propagation mechanism of the model.
The relationship between the model, view and controller and their respective main functions are shown in Figure 1.

2 Application Framework
In order to conveniently implement business process control and changes, this paper proposes an extensible framework based on the MVC design pattern. The advantages of this framework are:
1) It realizes
the packaging of the business process control part, which is convenient for simplifying the development of web applications. When using the framework to develop web applications, you only need to write web pages and business logic according to the page interface, and the control part is implemented by the framework according to the configuration file. This design is conducive to the collaboration between page artists and programmers.
2) It improves the expansion and reuse of web pages and business logic, facilitates the testing of business logic, and improves the flexibility of the system.

The structure of the framework is shown in Figure 2. Views and ViewManager are the view part of the system, among which ViewManager is responsible for page generation and display. Web services and business logic components provide the business logic of the application and belong to the model part of the system. The rest belongs to the controller part of the system. The main functions of each part are as follows: Manager provides a population to the application, and decides to create or load a task according to different Navigators: Navigator is responsible for managing the jump between pages, selecting the appropriate page according to the configuration file, and calling ViewManager to activate the page; ControllerBase and Controllers call different business logics according to the page request and return the result; State saves the state of the application and the currently displayed page.

The specific workflow of this application framework is as follows: After starting the task in the Manager, instantiate the navigator and pass appropriate information to the navigator. The navigator calls the Configuration class to retrieve the configuration information from the configuration file, and creates the Views Manager based on the configuration information. Then, the Manager calls the NavigateEvent event of the Navigator to obtain the page that the application wants to display and the next page to be displayed. At this time, you can call the Save method in the State class to save the state to the database. In addition, the navigator can also call the ActiveView method in the appropriate view manager to pass the view name to the view manager. The ActiveView method is responsible for creating a controller and displaying the view. After the view is displayed, it waits for the user to perform an action in the interface. If the user performs an action, the business logic is called through the controller, and the result will be returned to the page after processing.
Using the framework to develop applications can easily control and change business processes. At this time, you only need to modify the configuration file; you can implement state management, maintain the consistency of the state between pages, capture the state of the user's interaction with the application, and based on the saved state information, the user can continue the unfinished operation at any time, place, and machine.

3 Web program development for application framework
The business logic of the ERP system of automobile 4S stores is complex and the business process is easy to change. The framework proposed in this paper is used for development, which has a clear structure and is convenient for division of labor and collaboration, and achieves good results. We use the automobile parts purchase order module to illustrate the application of the framework, and its process is shown in Figure 3.

During the development process, the web pages to be displayed and the background business logic are written separately. In the configuration file, the view element is used to match the page name with the page code, and the page flow is written in the navigation part. If you want to modify the business process, you only need to modify the navigation part of the configuration file; in addition, adding pages is also very convenient. You only need to add a page node in the configuration file and configure the page flow process.
Some process configuration codes are as follows:


Through the configuration file settings, the framework can easily modify and expand the application, and realize the control and modification of the business process. In this way, web pages and logic codes can be easily modified and reused, and the application system also has strong flexibility and scalability.


4 Conclusion
As a commonly used development model, MVC is widely used in the design and analysis of distributed systems because it realizes the separation of data layer and presentation layer. It has high application reuse and is easy to maintain and modify. Many traditional web application developments with increasingly strong interactivity (such as PHP, ASP, JSP, etc.) have problems such as complex code, low reuse, and difficulty in maintenance and expansion. The extensible design framework based on the MVC idea can effectively improve the development efficiency of web applications.

Reference address:Design of Web Application Framework Based on Model-View-Controller

Previous article:Design of CAN bus node based on MCP2502X/05X
Next article:Design of spatial distributed temperature and humidity measurement and control system based on modbus protocol

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号