Author: Jiangnan Baiyi
Skip this article for fans of struts and webwork.
Does MVC separate M, V, and C? The simplest approach is that Two JSPs are responsible for the view, one is responsible for the Controller, and the other is responsible for the model Java Bean, which can already work well. At that time, everything was very simple.
Now, for some non-essential functions, so many non-standard Web frameworks are depressing. It is as simple as Ruby on Rails for development and can be used without many restrictions. For exampleWebworkThis type can also be considered. However, the more you use a framework like struts, the more troublesome it is, or there is a serious autistic tendency like tapestry. If you stick to the "special toys for experts", it is irresponsible to use them in the team.
So,
My MVC solution is to use the Controller Interface of spring MVC to write the most common JavaBean as the ControllerIn essence, it is similar to using JSP as controller in the past, but it has the characteristics of Spring IoC. The reason why such negative selection criteria are used is that this generation of MVC framework is doomed to be short-lived, transitional technology and is not worth investing too much energy and team learning costs.
1. Principle
Spring MVC belongs to the static configuration-type front controler in Martin flower's "Enterprise Application Mode" according to plant species, and uses dispatchservlet to intercept all *. the do request calls the handlerequest (request, response) function of the corresponding command object according to the XML file configuration, and injects the dependent object at the same time.
Our controller layer is a general JavaBean that implements the handlerequest (request, response) function.
2. Advantages
Advantages of spring MVC compared with Struts:
First, its controller has a loose to tight class hierarchy. You can choose to implement an interface with only one handlerequest () function, or use the simpleformcontroller class with many callback functions. Second, there is no need for form beans, or the so-called object-oriented page objects of tapestry. For those who are afraid of class expansion, it is best to change one thing and want to change to another place. Third, you do not need to emphasize xml configuration files. Declarative Programming is good, but if it is forced into a framework, everything should be declared in XML, Which is cumbersome to write, when reading the code, you need to configure the code on both sides to understand the problem. What about webwork? I have never tried it in practice, but I don't need to ask much about the MVC Framework. If I use spring MVC controller alone to meet my needs, I don't need to do a set of webwork to help my team, it also makes it troublesome to upgrade the versions of spring and WW2. Spring MVC has few source code and is easy to control and expand.
3. Simplification
3.1. directly implement controller to implement the handlerequest () functionFirst of all, simple form controller is not as good as mine. It is not simple at all. So sometimes I will directly implement the Controller Interface. The only function of this interface is handlerequest (request, response) called by the Front Controller ).
If you need an application object, for example, to use application. getrealpath (), you need extends webapplicationobjectsupport.
. Each controler is responsible for a group of related actionsI firmly support one controler responsible for multiple actions. One controler and one action are as boring as one function and one class. Therefore, in the most traditional way, I use URL parameters such as MSG = "insert" to assign a group of related actions to a controler. Ror and groovy on rails are both in this mode. Spring also supports multiactioncontroller.
The above three functions directly reflect the URL parameter as the controller function, and the stripes design can use annotation to mark the URL ing between the URL action and the response function.
3.3.xml Declarative ProgrammingMy trade-off is very simple. Spring does not have any force. I only need to dynamically inject things in XML when it is necessary to change something without re-compiling. JSP paths are all taken back to the Controller.3.4.data BinderData binder is an essential part of the controller. For the databinder provided by spring, it is fully available. The only unpleasant thing is that if an object has an embedded object, such as an order object that contains a customer object, spring requires you to create a customer object and assign it to the order object to implement Order. customer. customer_no. I was lazy and took Jakarta beanutils out and made a binder myself.
3.5. Extract base classesFinally, I couldn't help extracting a base class, responsible for multiaction and other simple methods. Sprnig's multiactioncontroller is too dead. It requires that the first and second parameters of all functions must be request and response. It does not understand dynamic parameters and is gentle in parameter injection.
After simplification and simplification, it is already a very simple Java Bean. Anyone can easily get started with it. Even if the tide of technology in a certain month has overwhelmed all MVC frameworks, and no one knows how to maintain it.
Related Articles
Simplified spring (1) -- configuration file
Simplified spring (2) -- model layer
Simplified spring (3) -- controller Layer
Simplified spring (4) -- View Layer