SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynami
Posted shamgod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynami相关的知识,希望对你有一定的参考价值。
一、
1.如想在DispatcherServlet在Servlet容器中注册后自定义一些操作,如开启文件上传功能,则可重写通过AbstractAnnotationConfigDispatcherServletInitializer 的customizeRegistration() 来实现
1 // After AbstractAnnotation ConfigDispatcherServletInitializer registers DispatcherServlet with the servlet 2 // container, it calls the customizeRegistration() method, passing in the Servlet- 3 // Registration.Dynamic that resulted from the servlet registration. By overriding 4 // customizeRegistration() , you can apply additional configuration to DispatcherServlet . 5 // With the ServletRegistration.Dynamic that’s given to customizeRegistration() , 6 // you can do several things, including set the load-on-startup priority by calling set- 7 // LoadOnStartup() , set an initialization parameter by calling setInitParameter() , and 8 // call setMultipartConfig() to configure Servlet 3.0 multipart support. 9 @Override 10 protected void customizeRegistration(Dynamic registration) { 11 registration.setMultipartConfig( 12 new MultipartConfigElement("/tmp/spittr/uploads", 2097152, 4194304, 0)); 13 }
2.通过重定WebApplicationInitializer的onStartup来注册servlet
1 package com.myapp.config; 2 import javax.servlet.ServletContext; 3 import javax.servlet.ServletException; 4 import javax.servlet.ServletRegistration.Dynamic; 5 import org.springframework.web.WebApplicationInitializer; 6 import com.myapp.MyServlet; 7 public class MyServletInitializer implements WebApplicationInitializer { 8 @Override 9 public void onStartup(ServletContext servletContext) 10 throws ServletException { 11 Dynamic myServlet = 12 servletContext.addServlet("myServlet", MyServlet.class); 13 myServlet.addMapping("/custom/**"); 14 } 15 }
is a rather basic servlet-registering initializer class. It registers a servlet and maps it to a single path. You could use this approach to register DispatcherServlet manually. (But there’s no need, because AbstractAnnotationConfigDispatcher-
ServletInitializer does a fine job without as much code.)
3.通过重定WebApplicationInitializer的onStartup来注册filter
1 @Override 2 public void onStartup(ServletContext servletContext) 3 throws ServletException { 4 javax.servlet.FilterRegistration.Dynamic filter = servletContext.addFilter("myFilter", MyFilter.class); 5 filter.addMappingForUrlPatterns(null, false, "/custom/*"); 6 }
4.To register one or more filters and map them to DispatcherServlet , all you need to do is override the getServletFilters() method of AbstractAnnotationConfigDispatcherServletInitializer .
1 @Override 2 protected Filter[] getServletFilters() { 3 return new Filter[] { new MyFilter() }; 4 }
As you can see, this method returns an array of javax.servlet.Filter . Here it only returns a single filter, but it could return as many filters as you need. There’s no need to declare the mapping for the filters; any filter returned from getServletFilters() will automatically be mapped to DispatcherServlet
以上是关于SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynami的主要内容,如果未能解决你的问题,请参考以下文章
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@ScopeProxyMode
SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect
SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMappingmodel
SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder