星空网 > 软件开发 > Java

springmvcController源码简单解析

前面简单的分析了一下DispatcherServlet,接下来分析一下Controller,在web的MVC中,Controller就是其中的C,启动的一些页面逻辑处理,页面映射的功能:

首先看看超类:

public interface Controller {
//处理请求,最后返回一个ModelAndView对象,这里的ModelAndView就是我们前面分析过:在DispatchServlet中的doDispath()这个方法里面
//会通过render方法得到ModelAndView对象,如果返回一个null,并不代表这是错误的,而是表示这个request已经执行完了,所以不能够
//通过render方法得到ModelAndViewModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;}

上面已经讲得很清楚了。就是在DispatcherSerlvet中,每一个request请求都会被拦截,然后在根据各自配置的bean在Controller走一遭,返回一个ModelAndView。

在Controller的子类有一个需要看一下,就是:

public abstract class AbstractController extends WebContentGenerator implements Controller {

我们看到它不仅继承了Controller,还继承了 WebContentGenerator,那我们先看看这个类都是干嘛的:

源码:

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
***/}

明白了,其实就时对缓存的一系列操作。下面对重要的属性分析一下:

1.supportedMethods:表示支持的请求方法,我们可以看到前面已经定义了三个常量,不错,这三个就是默认的请求方法,我们可以自己添加所支持的请求方法。

2requireSession:当前的这个请求是否必须session,如果设置了必须,那么请求当没有带session的话,是会报错的。

3 后面的这几个都是关于缓存的操作,包括HTTP1.0和HTTP1.1

4.alwaysMustRevalidate :如果一个处理器继承了LastModified,那么最好是设置为true,他能自动的计算lastModified。不过如果一个处理器没有继承,即使设置了true

也是没有用的。

从上面的分析知道了WebContentGenerator的作用,也就是说,WebContentGenerator有的功能,AbstractController也会具有这些的功能。除了这些功能外,

AbstractController很重要的一个功能就是继承Controller的那个方法,

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.  
    //串行化设置 if (this.synchronizeOnSession) { HttpSession session = request.getSession(false); if (session != null) { Object mutex = WebUtils.getSessionMutex(session); synchronized (mutex) { return handleRequestInternal(request, response); } } }    //AbstractController提供的模板方法,子类的实现都是写在其中了。 return handleRequestInternal(request, response); }

至于其他的AbstractController的子类,其实就是springmvc不同业务开发出来了Controller,以供我们可以根据自己的业务需要来进行调用。这里就不分析了。提供一个自己实现的Controller参考:

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;  }}

配置文件:

   <bean id="/index" class="com.hotusm.controller.TestController"/>    <!-- 这里配置视图解析器 -->  <!-- HandlerMapping -->   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>   <!-- 视图适配器 -->  <!-- 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>

这种都是以前的用法,也就是为了理解,才会那么些,现在的项目大部分都是利用注解的形式了,

 




原标题:springmvcController源码简单解析

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

TikTok Shop发布两大“口碑经营”激励计划!:https://www.goluckyvip.com/news/9353.html
东南亚直播电商市场爆发式增长,出海浪潮下把握趋势及赛道机遇 :https://www.goluckyvip.com/news/9354.html
十年深耕拖鞋类目!POSEE如何快速登上TikTok Shop跨境鞋靴第一? :https://www.goluckyvip.com/news/9355.html
外贸独立站建站(谷歌建站步骤) :https://www.goluckyvip.com/news/9356.html
TikTok Shop店铺差评率过高的主要原因 :https://www.goluckyvip.com/news/9357.html
外贸工厂转型TikTok,半年做到品牌榜第一:https://www.goluckyvip.com/news/9358.html
宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流