스프링 MVC 구성 요소 소스 코드 분석

구성 요소 개요

처리기 매핑

핸들러는 요청 인터셉터있어서 대응하는 처리를 찾아. 내부 한 방향으로 만

HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;

핸들러 어댑터

어댑터 처리기, 다음 내부 방법 :

boolean supports(Object handler);//判断是否可以使用某个 Handler
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; //具体使用
long getLastModified(HttpServletRequest request, Object handler);//获取资源上一次修改的时间

HandlerExceptionResolver

의 ModelAndView하고 렌더링 렌더링 방법에 따라 예외 설정.

ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)

의 ViewResolver

보기의 문자열 타입으로 사용 및 뷰의 로케일 유형을보기로 결심.

View resolveViewName(String viewName, Locale locale) throws Exception;

그것은 구현 클래스하는 BeanNameViewResolver이다가 resolveViewName는 다음 다시 작성

public View resolveViewName(String viewName, Locale locale) throws BeansException {
        ApplicationContext context = getApplicationContext();
        //如果应用上下文没有找到视图,返回 null
        if (!context.containsBean(viewName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("No matching bean found for view name '" + viewName + "'");
            }
            // Allow for ViewResolver chaining...
            return null;
        }
        //如果找到的视图类型不匹配,也返回 null
        if (!context.isTypeMatch(viewName, View.class)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found matching bean for view name '" + viewName +
                        "' - to be ignored since it does not implement View");
            }
            // Since we're looking into the general ApplicationContext here,
            // let's accept this as a non-match and allow for chaining as well...
            return null;
        }
        //根据视图名称从 Spring 容器中查找 Bean,返回找到的 bean
        return context.getBean(viewName, View.class);
    }

RequestToViewNameTranslator

요청의 뷰 이름을 가져옵니다. 단 하나의 방법입니다 인터페이스 :

String getViewName(HttpServletRequest request) throws Exception; //根据 request 查找视图名

LocaleResolver의

요청을 구문 분석 로케일.

public interface LocaleResolver {
    //从 request 解析出 Locale
    Locale resolveLocale(HttpServletRequest request);
    //根据 request 设置  locale
    void setLocale(HttpServletRequest request, HttpServletResponse response, @Nullable Locale locale);
}

ThemeResolver

테마 해결

public interface ThemeResolver {
    //通过给定的 request 查找主题名
    String resolveThemeName(HttpServletRequest request);
    //根据给定的 request 设置主题名
    void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);
}

RequestContext.java 파일에 대한 주제 :

public String getThemeMessage(String code, String defaultMessage) {
        //获取主题的信息
        return getTheme().getMessageSource().getMessage(code, null, defaultMessage, this.locale);
    }

public Theme getTheme() {
        //判断主题是否为空
        if (this.theme == null) {
            // 通过 RequestContextUtils 获取 request 中的主题名
            this.theme = RequestContextUtils.getTheme(this.request);
            if (this.theme == null) {   //如果还是为空的话
                //那就是没有有效的主题解析器和主题
                this.theme = getFallbackTheme();
            }
        }
        return this.theme;
    }

RequestContextUtils.getTheme () 方法 :

public static Theme getTheme(HttpServletRequest request) {
        ThemeResolver themeResolver = getThemeResolver(request);
        ThemeSource themeSource = getThemeSource(request);
        if (themeResolver != null && themeSource != null) {
            String themeName = themeResolver.resolveThemeName(request);
            return themeSource.getTheme(themeName);
        }
        else {
            return null;
        }
    }

MultipartResolver

처리를 위해 업로드 요청, 처리 방법 : MultipartHttpServletRequest를 공통 포장 요청

public interface MultipartResolver {
    //根据 request 判断是否是上传请求
    boolean isMultipart(HttpServletRequest request);
    //将 request 包装成 MultipartHttpServletRequest
    MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
    //清理上传过程中产生的临时资源
    void cleanupMultipart(MultipartHttpServletRequest request);
}

FlashMapManager

FlashMap 차 배달 매개 변수를 리디렉션, FlashMapManager FlashMap 관리하는 데 사용됩니다.

public interface FlashMapManager {
    //恢复参数,并将恢复过的和超时的参数从保存介质中删除
    @Nullable
    FlashMap retrieveAndUpdate(HttpServletRequest request, HttpServletResponse response);
    //将参数保存起来
    void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest request, HttpServletResponse response);
}

개요

소개 스프링 MVC는 인터페이스의 구 개 구성 요소, 역할 및 내부 방법의 기능은 우리 모두가 소스 코드를 자세히 볼 필요 간략한 소개를 달성했다.

개요

스프링 MVC 원칙 요약

서블릿, HttpServlet을 상속 서블릿은 기본적입니다. HttpServletBean, FrameworkServlet과의 DispatcherServlet : 스프링 MVC는 서블릿의 세 가지 수준을 제공합니다. 그들은 직접 자바 HttpServlet을 상속 HttpServletBean 서로를 상속합니다. 서블릿 서블릿 파라미터는 각각의 특성을 설정하도록 구성 HttpServletBean, FrameworkServlet는 처리 요구 아홉 개 특정 성분은 다음과 같이 전체의 DispatcherServlet 연속 인 초기화하는 것, 스프링 MVC의 WebApplicationContext 사용 초기화 :

스프링 MVC 구성 요소 소스 코드 분석

추천

출처blog.51cto.com/14230003/2425544