The MVC architecture implements the basics:
The request address analysis is implemented based on the filter or servlet, and the corresponding control class is invoked if the class processing request needs to be controlled. When the control class is called, the control class-related parameters are initialized according to the configuration file. Database connections can persist. The control class finishes processing, returns the data as well as the template file, renders the template file, and returns it to the client.
Step simplification, there are 4 steps: 1, according to the configuration file or annotations, initialize all the control classes, complete the URL and Control class mapping, 2, receive the request, judge the control class used, 3, inject the control class or method required parameters, call the corresponding control class or method processing, return data and template file address; 4, The render template file is returned to the client based on the returned data and template file address.
This is the basic MVC architecture that must be implemented by the process, performance issues through modification, can basically reach the same level.
Controls the instantiation of a class, depending on the reflection mechanism. By using reflection, we know the properties of the control class and the parameters of the method, when invoking the method of the control class, we can initialize the correlation parameter and execute the control class method according to the parameter name and the corresponding relation of the request parameter. After the control class method executes, it returns the type of the contract, such as a string, the template file to be used, or the redirected address; spring's Modelandview, which contains data and template files; Map, which contains data, uses the default template file, void, no data, Just call the default template file. By judging the return value type, the MVC framework can perform the agreed behavior, complete the final view data assembly, and return it to the client.
Common Java-based MVC architectures have structs and struct2,spring MVC. Early spring many as a struct control class instantiation module exists, in Web. XML is used in the listener to complete all the bean initialization, dependency injection and other operations, but the configuration file is cumbersome to write. The configuration file of a struct, which needs to be mapped to the request address and the control class, is written in a configuration file and is cumbersome to write.
In large web site development, multi-person collaboration, centralized writing of the same configuration file, will create a conflict, it is necessary to use a relatively simple way to implement, the request address and Control class mapping relationship configuration.
After the advent of Java annotations, Spring MVC uses annotations to initialize this mapping and is easy to implement thanks to collaborative development. In fact, the project structure is the configuration of the relationship mapping, to note the special treatment of the way, should be more easy to accept.
The template file returned, the struct used a custom label, the intrusion is severe, not easy to reuse. You should use one of the easiest ways to hack and modify the template as a form of writing.
Java MVC Architecture-spring Mvc,struct2 (understanding)