The Controller in the MVC model is responsible for parsing user input information and passing the transformation into a model, which may be presented to the user initiating the request. Spring uses
The abstract method reflects the Controller concept, so developers can have multiple choices when creating the controller. Spring contains three types of controllers: Processing HTML Forms
Controller, command-based controller, and wizard-style controller.
In spring, the basic controller class is org. springframework. Web. servlet. MVC. Controller. This is a very simple interface and the source code is as follows:
Package org. springframework. Web. servlet. MVC;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. springframework. Web. servlet. modelandview;
Public interface controller {
Modelandview handlerequest (httpservletrequest request, httpservletresponse response) throws exception;
}
The Controller Interface defines only one method to process customer requests and return appropriate models and views. This is also the responsibility of all controllers.
Modelandview and controller. This is spring
The basis of MVC Framework implementation. Although controller is rather abstract, Spring provides multiple Controller Interface implementation classes.
1. abstractcontroller class
Abstractcontroller is a subclass of webcontentgenerator and implements the Controller Interface.
Abstractcontroller is one of the most important Controller Interface implementations.
It provides some basic features, such as generating the HTTP protocol cache header data and setting get/post actions.
Measure the test taker's knowledge about the class hierarchy of abstratorcontroller:
Java. Lang. Object
| _ Org. springframework. Context. Support. applicationobjectsupport
| _ Org. springframework. Web. Context. Support. webapplicationobjectsupport
| _ Org. springframework. Web. servlet. Support. webcontentgenerator
| _ Org. springframework. Web. servlet. MVC. abstractcontroller
Abstratorcontroller inherits many attributes from its superclass. These attributes can be injected through the configuration file:
* Supportedmethods: Specifies the method that the Controller should accept. The default value is "get, post". developers can modify this attribute to reflect the method to be supported. If a request is configured with this method but not supported by the Controller, this information will be notified to the customer.
* Requiressession: indicates whether the controller needs an http session to complete its work. If contreller does not receive an http session when receiving a request, it will throw a servletexception. The default value of this attribute is false.
* Synchronizesession: This attribute is used if the Controller needs to be processed synchronously in the client's HTTP session.
* Cacheseconds: When the controller needs to generate a cache command for the customer's HTTP response, it can specify a positive integer for cacheseconds. The default value of this attribute is-1, that is, no cache is set.
* Useexpiresheader: instructs the Controller to specify an "expires" header data compatible with HTTP 1.0 for the customer's HTTP response. The default value of this attribute is true.
* Usecacheheader: instructs the Controller to specify a "cache-control" header data compatible with HTTP 1.1 for the customer's HTTP response. The default value of this attribute is true.
Let's take a look at the source code of abstractcontroller in the spring src directory:
Package org. springframework. Web. servlet. MVC;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Import org. springframework. Web. servlet. modelandview;
Import org. springframework. Web. servlet. Support. webcontentgenerator;
Import org. springframework. Web. util. webutils;
Public abstract class abstractcontroller extends webcontentgenerator implements controller {
Private Boolean synchronizeonsession = false;
Public final void setsynchronizeonsession (Boolean synchronizeonsession ){
This. synchronizeonsession = synchronizeonsession;
}
Public final Boolean issynchronizeonsession (){
Return synchronizeonsession;
}
Public final modelandview handlerequest (httpservletrequest request, httpservletresponse response)
Throws exception {
Checkandprepare (request, response, this instanceof lastmodified );
If (this. synchronizeonsession ){
Httpsession session = request. getsession (false );
If (session! = NULL ){
Object mutex = webutils. getsessionmutex (session );
Synchronized (mutex ){
Return handlerequestinternal (request, response );
}
}
}
Return handlerequestinternal (request, response );
}
Protected abstract modelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)
Throws exception;
}
From the code above, we can see that the workflow of abstractorcontroller is as follows:
1. dispatcherservlet calls the handlerequest method;
2. Check the supported methods (one of get/post/Put). If not, servletexception is thrown;
3. If a session needs to be initiated, an attempt is made to obtain a session. If the session cannot be obtained, servletexception is thrown;
4. Set the cache header data based on the cacheseconds attribute;
5. Call the protected abstract method handlerequestinternal. This method should be implemented by the abstractcontroller subclass and return the modelandview object.
When developers use abstractcontroller as their own controller base class, they only need to overwrite
Handlerequestinternal (httpservletrequest,
Httpservletresponse) method, and return a modelandview object, for example:
Package samples;
Public class samplecontroller extends abstractcontroller {
Public modelandview handlerequestinternal (
Httpservletrequest request,
Httpservletresponse response) throws exception {
Modelandview = new modelandview ("hello ");
Modelandview. addobject ("message", "Hello world! ");
Return modelandview;
}
}
The definition example in the configuration file is as follows:
<Bean id = "samplecontroller" class = "samples. samplecontroller">
<Property name = "cacheseconds" value = "120"/>
</Bean>
In this example, if samplecontroller is enabled, 120 seconds of cache will be specified for the client's HTTP response. Samplecontroller returns an encoded view (this is generally not recommended ).
2. Other simple Controllers
Although developers can expand abstractcontroller by themselves, Spring provides many specific implementations that can be used for simple MVC applications.
The parameterizableviewcontroller class is basically the same as the preceding example. Except that developers can specify the name of the returned view, you do not need to write the view name in the Java class.
Urlfilenameviewcontroller checks the URL, finds the file name of the file request, and uses it as the view name. For example
The file name is "Index ".