Compare of Django and SpringMVC - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki

The following content is writtern by Group California.

Group: California

Frameworks: Django and SpringMVC

Group Members:

  1. Zhang Yiqun(@YiqunZhang)
  2. Xu Yuang (@itwai)
  3. Zhao Yumeng (@15203438975)
  4. Song Shengjun (@songkoukou)
  5. Yang Zhuoqun (@Malcolm-Yang)
  6. Wang Yidan (@Oneydauh)
  7. Zhao Chuan (@zhao-chuan-collab)
  8. Zeng Guangming (@California3)
  9. Tian Yuan (@MephistoG)
  10. Zhang Jingyu(@Zhangjingyu-Terry)

Django

Basic Infomation

Django is an open-source Web development framework written in Python language and designed according to MVC. It was written by a network development team in Lawrence City, Kansas State. It was born in the fall of 2003, when Adrian holovaty and Simon Willison, programmers of the Lawrence journal world newspaper, began programming in Python.

Architecture - Django Tutorial - Edureka

Django mainly serves for Web development, so Django's future is closely related to the future development trend of Web development. Django, a traditional framework based on MVC development pattern, is very suitable for developing traditional websites based on PC, because it includes both back-end development (logic layer, database layer) and front-end development (such as template language, style). PC-based websites and responsive mobile websites will not disAPPear, but their importance will gradually decrease with the popularity of mobile app and small programs. Even for the development of traditional websites, Django has some advantages only in the background development. In the front end, it is still necessary to use popular JS frameworks such as Vue.js to develop excellent websites with strong interactivity and in line with future aesthetic trends.

mvc - Django Tutorial - Edureka

The Django framework was originally mainly used to develop websites, but with the development of mobile Internet, Python and Django are increasingly used in the background development languages of various mobile APP. There is usually only one background for modern Web APPlication web app, and then the clients are various (iOS, android, browser), and the development language of the clients is probably different from that of the background.

At this time, we need the background to provide a standard data exchange format (JSON or XML) that can cross platforms and languages for front-end and back-end communication. This is the Web API (Network Interface). With the maturity of django-rest-framework, it becomes very easy to use Django as the background to provide REST-compliant Web API(json format data) to interact with different clients (iOS, android).

The more popular development mode in recent years and the next few years is definitely separation of front and back ends. Modern Web APPlications such as web app or large websites are usually a backstage and then correspond to various clients (iOS, android, browser). Since the development language of the client is often different from that of the background, the background is required to provide a standard resource or data (such as Json format) that can cross platforms and languages for communication between the front and back ends. This is the role of Web API (Web Application Interface). Django itself cannot develop a Web API conforming to REST specifications, but with the help of django-rest-framework (DRF), it can quickly develop a Web API of excellent specifications.

Therefore, we would like to thank DRF here, because it has a wider application prospect for Django and reduces the risk of being eliminated. Python is the soil Django lives on. At present, Python is still very hot in various fields of Web development, scientific computing and artificial intelligence. From this point of view, Django should have no crisis in the next five years.

Architecture

Django follows a MVC- MVT architecture.

MVC

MVC stands for Model View Controller. It is used for developing the web publications, where we break the code into various segments. Here we have 3 segments, model view and a controller.

mvc - Django Tutorial - Edureka

Model --  Model is used for storing and maintaining your data. It is the backend where your database is defined.

Views -- In Django templates, views are in html. View is all about the presentation and it is not at all aware of the backend. Whatever the user is seeing, it is referred to a view.

Controller -- Controller is a business logic which will interact with the model and the view.

Django's MVC based design

The core components of the Django framework are:

  1. Object relational mapping used to create the model;

ORM (object-relational mapping) : define your data model as a Python class. ORM connects the model to a relational database, and you get a database API that is very easy to use. You can also use raw SQL statements in Django.

  1. Design a better management interface for end users;

Automated management interface: you don't have to spend a lot of work creating people management and updating content. Django comes with an ADMIN site, similar to a content management system.

  1. URL design;

URL dispatch: using regular expressions to match urls, you can design arbitrary urls without the framework's specific constraints. Be as flexible as you like.

  1. Designer friendly template language;

Template system: use Django's powerful and extensible template language to separate design, content, and Python code. And it's inheritable.

  1. Caching system.

Cache system: can be hung in memory buffer or other framework to achieve super buffer - achieve the granularity you need. Session, user login and permission check, rapid development of user session function.

  1. Other

Form processing: you can easily generate a variety of form models to achieve form validation. You can easily generate the corresponding form from the model instance you define.

Internationalization: the built-in internationalization system facilitates the development of multi-language websites.

MVT

MVT stands for Model View Template. In MVT, there is a predefined template for user interface. With template, you will be having only one file that prints hello along with the variable name. Now this variable will be substituted in that particular template using some jinja logic. That's the magic of template, you don't need to rewrite the code again and again!

Architecture - Django Tutorial - Edureka

In the above image, template is your front end which will interact with the view and the model will be used as a backend. Then view will access both the model and the templates and maps it to a url. After that, Django plays the role of controller and serves it to the user.

In Django, the part of the controller that accepts user input is handled by the framework itself, so Django focuses more on models, templates, and Views, known as the MTV pattern. Their respective responsibilities are as follows:

Hierarchical responsibility
The Model, or data access layer handles all transactions related to the data: how it is accessed, how it is validated, what behaviors are included, and the relationships between the data.
Templates, or presentation layers handle performance-related decisions about how to display in a page or other type of document.
View, the logic that allows the business logic layer to access the model and retrieve the appropriate template Bridge between model and template.

SpringMVC

Basic Infomation

SpringMVC is a member of the Spring family. And Spring is a framework that combines the components that are popular in development now! It is used in the development layer based on MVC, similar to struts2 framework.

Spring MVC core component

  1. dispatcher servlet: front-end controller, core role: receive request, response results, equivalent to forwarder, central processor, reducing the coupling between components. The user sends the request to dispatcherservlet, which is the center of the whole process control. The dispatcherservlet calls other components to process the user request and distributes it to the specific corresponding controller, so as to obtain the required business data model, which is then passed to view through dispatcherservlet to complete the page presentation; the existence of dispatcherservlet reduces the coupling between components ใ€‚

  2. Handlermapping: processor mapper function: find the corresponding handler according to the request URL, help dispatcherservlet find the corresponding controllerhandlermapping, which is responsible for finding the handler or processor according to the user request. Spring MVC provides a variety of different mappers to implement different mapping methods, such as: configuration file method, implementation interface method, annotation method, etc.

  3. Handlerinterceptor: the handlerinterceptor before and after the execution of the handler handlerinterceptor is an interface, which contains three methods: prehandle, posthandle and aftercompletion, which are executed before, during and after the execution of the handler.

  4. Handlerexecutionchain: handlermapping the execution chain returned to the dispatcherservlet handlermapping is returned to the dispatcherservlet There are not only handlers, but also handlerinterceptorprehandle - > controllermethod - > posthandle - > aftercompletion. How is this chain implemented? The reflection mechanism of Java is used: reflection

  5. handleradapter: processor adapter function: adapts various controllers to the handler that can be used by dispatcherservlet, executes the handler through specific rules (the rules required by handleradapter), and executes the handler through handleradapter. This is the application of adapter mode, and more types can be implemented through extension adapter To execute.

  6. Handler: processor (need to be developed by engineers). Note: when writing a handler, you need to do what the handleradapter requires, so that the handleradapter can correctly execute. The handlerhandler is the background controller following the dispatcherservlet front-end controller, and processes user requests under the control of the dispatcherservlet. The handler involves business requirements, so engineers need to focus on users Requirements are developed, and finally business data is returned.

  7. Modelandview: a representation of model in spring MVC. There are models and maps in spring MVC, but spring MVC will convert them into modelandview. Both models and maps are the concrete representation of modelandview.

  8. Viewresolver: View resolver function: perform view resolution, and resolve them into real viewviewres according to the logical view name The resolver is responsible for generating view view from the processing results. The viewresolver first resolves the processing results into specific page address according to the logical view name, then renders the view, and displays the processing results to the user through the page. Spring MVC provides many types of view views, including jstlview, freemarkerview, pdfview, JSP, HTML, etc.

  9. View: view (requires engineers to develop JSP and HTML) view is an interface, and implementation classes support different types (JSP, HTML, FreeMarker, PDF, etc.)

Compare

  • Django is in itself a highly customizable web framework. The database, template framework and ORM can all be swapped out. However, if you are looking to build a quick prototype fast and easy, SpringMVC isn't going to help much. It's very large and quite hard to grasp if you are just beginning with it.

  • Django's philosophy of batteries included means that experienced developers won't have to plan too much ahead on what kind of application infrastructure they need and instead just start developing web applications quickly. However, when using SpringMVC, updating and code maintenance can be a grueling task if you are a beginner.

  • Using Django, just a few lines of code can instruct Django to create all the tables and fields required in your database automatically. Schemas are managed with "migrations", that are also created automatically, and can be rolled out from your development box and implemented on production systems with just a single command.

  • SpringMVC is convoluted and XML-ridden. Deploying things on the JVM has always been a pain in the nipple and nothing has changed much since the 90s. All things Java-related are wordy, verbose, and a waste of developer time.

  • Django's philosophy of batteries included means that experienced developers won't have to plan too much ahead on what kind of application infrastructure they need and instead just start developing web applications quickly.

  • Django has a powerful database access component. Django's Model layer comes with a database ORM component, so that developers can operate the database without learning the SQL language.

  • Django comes with its own platform management system admin: only a few lines of configuration and code are needed to achieve a complete background data management control and the platform Django debug information is exhaustive, it is easy to find out where the code is wrong.

  • Django has many powerful plugins which are good with UI development. While the SpringMVC is actually very good and with a very complete and rich feature set to develop and maintain code on the server side, it still doesn't provide any rich framework for building good user interfaces.

  • Django has a great authentication and authorization system. It's easy to use and powerful. If using SpringMVC, it will be really complex and bloated.

  • Django has the advantages of perfect functions and complete elements: it comes with a large number of commonly used tools and frameworks (such as paging, auth, permission management) and is suitable for rapid development of enterprise websites.

  • Perfect Documents: After more than ten years of development and improvement, Django has a wide range of practical cases and perfect online documents. Developers can search online documents for solutions when encountering problems.

  • Powerful database access component: Django's Model layer has its own database ORM component, which enables developers to operate the database without learning SQL language.

  • DJango's Advanced App Design Concept: App is pluggable and is a rare thought. No need, it can be deleted directly and has little influence on the whole system.

  • Bring your own platform management system admin: Only a few simple lines of configuration and code are needed to realize a complete background data management and control platform.

  • Django debug has detailed information: it is easy to find out where the code error is.

Coding experience and Conclusion

  • In terms of program structure, the relative definition of Java can be their own constraints and configuration, and can completely according to our own company's structure and habits to write. Django is also relatively similar, for static directory through the system's OS, path and setting configuration can specify the corresponding directory as a static directory or file upload directory and similar functions.

  • In terms of system function planning, the working directory generated through Django will provide basic programs, including the initial wsgi, setting, url and other basic project files. In addition, Django manager startapp will generate the corresponding app directory structure in the same directory. Generally speaking, the file structure is generally fixed.

  • It is worth mentioning that Python's TAB spacing habit makes the code clear at a glance, which improves the readability of the code.

  • Development processes and templates: Django can generate module code by command, while Java canโ€™t. Some common ListView, DetailView, and TemplateView are also encapsulated in the development of Django to reduce the amount of code being developed. After generating the module code, Django can register with admin.py in app via @through the definition of the built-in Model layer to enter the background management of the system. The web interface can manage data without any development. This, I believe, is one of the most powerful and maligned aspects of Django. The advantage is that the model can be injected into the management platform without any code development. The disadvantage is that the management platform is very rigid and difficult to modify, such as modifying the template style in the background and adding some common processing to template variables (such as replacing {{temp}} from 1 to "male", which requires writing code to achieve). You can't use Python's functionality in a template because you need to conform to the template's Api syntax.

  • Related plugins and so on: Java's strength is in the system architecture such as workflow module, system permission module, data cache, sharding, microservices and other architecture levels. However Django is more approachable for everyday business code development, so it is more advantageous for developing mid-to-low scale systems.