Springmvc (2) simple Controller source code parsing and springmvc source code parsing

Source: Internet
Author: User

Springmvc (2) simple Controller source code parsing and springmvc source code parsing

The previous section briefly analyzed the DispatcherServlet, and then analyzed the Controller. In web MVC, Controller is the C among them. Some page logic processing and page ing functions are started:

First, let's look at the superclass:

Public interface Controller {
// Process the request, and finally return a ModelAndView object. The ModelAndView here is the one we analyzed earlier: In the doDispath () method in DispatchServlet
// The ModelAndView object will be obtained through the render method. If a null value is returned, this does not mean this is incorrect, but indicates that the request has been executed, so it cannot
// Get ModelAndViewModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception through the render method ;}

The above is clear. In DispatcherSerlvet, each request is intercepted, and a ModelAndView is returned when the bean configured according to the request is routed to the Controller.

In the subclass of Controller, you need to check that:

public abstract class AbstractController extends WebContentGenerator implements Controller {

We can see that it not only inherits Controller but also WebContentGenerator. Let's first look at what this class is doing:

Source code:

public abstract class WebContentGenerator extends WebApplicationObjectSupport {    /** HTTP method "GET" */    public static final String METHOD_GET = "GET";    /** HTTP method "HEAD" */    public static final String METHOD_HEAD = "HEAD";    /** HTTP method "POST" */    public static final String METHOD_POST = "POST";    private static final String HEADER_PRAGMA = "Pragma";    private static final String HEADER_EXPIRES = "Expires";    private static final String HEADER_CACHE_CONTROL = "Cache-Control";    /** Set of supported HTTP methods */    private Set<String>    supportedMethods;    private boolean requireSession = false;    /** Use HTTP 1.0 expires header? */    private boolean useExpiresHeader = true;    /** Use HTTP 1.1 cache-control header? */    private boolean useCacheControlHeader = true;    /** Use HTTP 1.1 cache-control header value "no-store"? */    private boolean useCacheControlNoStore = true;    private int cacheSeconds = -1;    private boolean alwaysMustRevalidate = false;
/***
  some operation about attr
***/}

Now, we can see a series of operations on the cache. The following describes the important attributes:

1. supportedMethods: indicates the supported request methods. We can see that three constants have been defined before. Good, these three are the default request methods. We can add the supported request methods by ourselves.

2 requireSession: whether the current request requires a session. If it is set to a required session, an error is returned if the request does not contain a session.

These operations are related to caching, including HTTP1.0 and HTTP1.1.

4. alwaysMustRevalidate: If a processor inherits LastModified, it is best to set it to true to automatically calculate lastModified. However, if a processor does not inherit, even if it is set to true

It is useless.

From the above analysis, we know the role of WebContentGenerator. That is to say, AbstractController also has some functions of WebContentGenerator. In addition to these features,

An important feature of AbstractController is to inherit the Controller method,

Public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {// Delegate to WebContentGenerator for checking and preparing. checkAndPrepare (request, response, this instanceof LastModified); // Execute handleRequestInternal in synchronized block if required.
// Set if (this. synchronizeOnSession) {HttpSession session = request. getSession (false); if (session! = Null) {Object mutex = WebUtils. getSessionMutex (session); synchronized (mutex) {return handleRequestInternal (request, response) ;}}// template method provided by AbstractController. The implementation of subclass is written in it. Return handleRequestInternal (request, response );}

As for the sub-classes of other AbstractController, springmvc has developed controllers for different services, so that we can call them according to our own business needs. This will not be analyzed here. Provides a self-implemented Controller reference:

public class TestController extends AbstractController{        @Override    protected ModelAndView handleRequestInternal(HttpServletRequest request,            HttpServletResponse response) throws Exception {        ModelAndView mv=new ModelAndView();        System.out.println("=====");        mv.setViewName("index");        return mv;    }}

Configuration file:

<Bean id = "/index" class = "com. hotusm. controller. TestController"/> <! -- Configure the view parser here --> <! -- HandlerMapping --> <bean class = "org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping"/> <! -- View adapter --> <! -- HandlerAdapter --> <bean class = "org. springframework. web. servlet. mvc. SimpleControllerHandlerAdapter"/> <! -- ViewResolver --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/"/> <property name =" suffix "value = ". jsp "/> </bean>

This is a previous usage, that is, to understand. Most of the current projects are in the form of annotations,

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.