spring web application overview

glossary

request flow

image.png

DispatcherServlet

  • 根據(jù)pattern轉(zhuǎn)發(fā)request
  • 把request 轉(zhuǎn)發(fā)到Spring MVC Controller
  • 根據(jù)handler mappings 確定requests要經(jīng)過的component, mappings可以配置pattern
  • 根據(jù)view resolver解析具體的view
  • 啟動后創(chuàng)建Spring application context,加載配置的bean:
    • controllers
    • view resolvers
    • handler mappings

controller

  • 指定渲染的view

model

  • 產(chǎn)生需要返回客戶端的數(shù)據(jù)

view

  • render the output

ContextLoaderListener

加載middle-tier和data-tier components

配置mvc

spring web application 中一般會創(chuàng)建兩個application context:

  • DispatcherServlet
  • ContextLoaderListener

configuring dispatcherservlet

AbstractAnnotationConfigDispatcherServletInitializer(前提條件web服務(wù)器必須支持Servlet 3.0,Apache Tomcat 7以上) JavaConfig:

  • getServletMappings(): 指定map的path
  • protected Class<?>[] getServletConfigClasses(): 指定DispatcherServlet 創(chuàng)建的application context 使用的配置類
  • protected Class<?>[] getRootConfigClasses(): 創(chuàng)建的ContextLoaderListener application context 使用的配置類

enabling spring mvc

  1. DispatcherServlet 配置:
package spittr.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.
DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.
                                      WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.
                                         InternalResourceViewResolver;
@Configuration
@EnableWebMvc  
@ComponentScan("spitter.web")
public class WebConfig
       extends WebMvcConfigurerAdapter {
  @Bean
  public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver =
            new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}
  @Override
  public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
} }
  1. ContextLoaderListener
package spittr.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"spitter"},
    excludeFilters={
        @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
    })
public class RootConfig {
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容