菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
88
0

SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynamic、WebApplicationInitializer)

原创
05/13 14:22
阅读数 95345

一、

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

发表评论

0/200
88 点赞
0 评论
收藏
为你推荐 换一批