XML 搭建

  1. <web-app>
  2. <display-name>Archetype Created Web Application</display-name>
  3. <listener>
  4. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  5. </listener>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>WEB-INF/spring.xml</param-value>
  9. </context-param>
  10. <servlet>
  11. <servlet-name>mvc</servlet-name>
  12. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  13. <init-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>WEB-INF/web.xml</param-value>
  16. </init-param>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>mvc</servlet-name>
  21. <url-pattern>*.do</url-pattern>
  22. </servlet-mapping>
  23. </web-app>

注解配置

使用接口 WebApplicationInitializer 来实例化 Spring 容器 和 DispathServlet

  1. public class MyWebApplicationInitializer implements WebApplicationInitializer {
  2. // public class MyWebApplicationInitializer implements TestWebApplicationInitializer {
  3. /**
  4. * Spring MVC 0-XML 配置的原理:
  5. *
  6. * 因为 servlet 3.0 的一个新规范(ServletContainerInitializer),
  7. * 而 tomcat 也遵守了了这个规范,所以会调用 实现了 WebApplicationInitializer 的 onStartup 方法
  8. *
  9. * Spring 定义了一个 org.springframework.web.SpringServletContainerInitializer 类,实现了 servlet 3.0 的这个新规范
  10. *
  11. * Spring 实现方式
  12. * spring-web\META-INF\services\javax.servlet.ServletContainerInitializer,是指需要实现的接口
  13. * javax.servlet.ServletContainerInitializer 文件里面的 org.springframework.web.SpringServletContainerInitializer 是实现类
  14. * @HandlesTypes(WebApplicationInitializer.class) 会由容器进行解析里面定义的接口,找出所有实现类,并回调给 Set<Class<?>> webAppInitializerClasses
  15. * Spring 再对 webAppInitializerClasses 的子类进行遍历,分别调用其 onStartup 的方法
  16. *
  17. * @param servletContext Web上下文对象
  18. * @throws ServletException
  19. */
  20. public void onStartup(ServletContext servletContext) throws ServletException {
  21. System.out.println("-------onStartup-------");
  22. // Load Spring web application configuration
  23. AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
  24. ac.register(AppConfig.class);
  25. // ac.refresh();
  26. // Create and register the DispatcherServlet
  27. // DispatcherServlet 的 init 方法
  28. DispatcherServlet servlet = new DispatcherServlet(ac);
  29. ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
  30. registration.setLoadOnStartup(1);
  31. registration.addMapping("*.do");
  32. // registration.addMapping("/*");
  33. }
  34. }

使用 WebMvcConfigurer 来配置 Spring MVC 环境

  1. @Configuration
  2. @ComponentScan("org.wesoft.mvc")
  3. @EnableWebMvc // 相当于 XML 中的 <mvc:annotation-driven/>
  4. public class AppConfig implements WebMvcConfigurer {
  5. // public class AppConfig extends WebMvcConfigurationSupport {
  6. /*
  7. Apache Commons FileUpload
  8. To use Apache Commons FileUpload,
  9. you can configure a bean of type CommonsMultipartResolver with a name of multipartResolver.
  10. You also need to have commons-fileupload as a dependency on your classpath.
  11. */
  12. @Bean
  13. public CommonsMultipartResolver multipartResolver() {
  14. CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
  15. return commonsMultipartResolver;
  16. }
  17. // 会在 Spring mvc 环境初始化完成之后,加载这个方法
  18. @Autowired
  19. public void iniArgumentsResolvers(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
  20. List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>(requestMappingHandlerAdapter.getArgumentResolvers());
  21. // 获取自定义的参数解析器
  22. List<HandlerMethodArgumentResolver> customArgumentResolvers = requestMappingHandlerAdapter.getCustomArgumentResolvers();
  23. // 删除原来集合中的自定义参数解析器
  24. argumentResolvers.removeAll(customArgumentResolvers);
  25. // 重新加入到最前面
  26. argumentResolvers.addAll(0, customArgumentResolvers);
  27. // 重新设置参数解析器
  28. requestMappingHandlerAdapter.setArgumentResolvers(argumentResolvers);
  29. }
  30. /**
  31. * 视图处理
  32. *
  33. * @param registry
  34. */
  35. @Override
  36. public void configureViewResolvers(ViewResolverRegistry registry) {
  37. registry.jsp("/page/", ".html");
  38. }
  39. /**
  40. * 添加消息解析器
  41. *
  42. * @param converters
  43. */
  44. @Override
  45. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  46. FastJsonHttpMessageConverter jsonHttpMessageConverter = new FastJsonHttpMessageConverter();
  47. converters.add(jsonHttpMessageConverter);
  48. }
  49. /**
  50. * 添加参数解析器
  51. *
  52. * @param resolvers initially an empty list
  53. */
  54. @Override
  55. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
  56. // 这里其实是传过来一个空集合,对应着 customArgumentResolvers
  57. // 也就是我们其实是在 customArgumentResolvers 里添加自定义方法
  58. // 然后 spring mvc 会将这个集合合并到 argumentResolvers 中,所以并不能保证执行顺序
  59. resolvers.add(new LoginUserResolver());
  60. }
  61. }