SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynamic、WebApplicationInitializer)
一、
1.如想在DispatcherServlet在Servlet容器中注册后自定义一些操作,如开启文件上传功能,则可重写通过AbstractAnnotationConfigDispatcherServletInitializer 的customizeRegistration() 来实现
// After AbstractAnnotation ConfigDispatcherServletInitializer registers DispatcherServlet with the servlet
// container, it calls the customizeRegistration() method, passing in the Servlet-
// Registration.Dynamic that resulted from the servlet registration. By overriding
// customizeRegistration() , you can apply additional configuration to DispatcherServlet .
// With the ServletRegistration.Dynamic that’s given to customizeRegistration() ,
// you can do several things, including set the load-on-startup priority by calling set-
// LoadOnStartup() , set an initialization parameter by calling setInitParameter() , and
// call setMultipartConfig() to configure Servlet 3.0 multipart support.
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(
new MultipartConfigElement("/tmp/spittr/uploads", 2097152, 4194304, 0));
}
2.通过重定WebApplicationInitializer的onStartup来注册servlet
package com.myapp.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import com.myapp.MyServlet;
public class MyServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
Dynamic myServlet =
servletContext.addServlet("myServlet", MyServlet.class);
myServlet.addMapping("/custom/**");
}
}
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
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
javax.servlet.FilterRegistration.Dynamic filter = servletContext.addFilter("myFilter", MyFilter.class);
filter.addMappingForUrlPatterns(null, false, "/custom/*");
}
4.To register one or more filters and map them to DispatcherServlet , all you need to do is override the getServletFilters() method of AbstractAnnotationConfigDispatcherServletInitializer .
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new MyFilter() };
}
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.Dynamic、WebApplicationInitializer)的更多相关文章
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute("spitter", spitter);)
一.redirect为什么会丢数据? when a handler method completes, any model data specified in the method is copied ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-004- 处理上传文件
一.用 MultipartFile 1.在html中设置<form enctype="multipart/form-data">及<input type=&quo ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-007-表单验证@Valid、Error
一. Starting with Spring 3.0, Spring supports the Java Validation API in Spring MVC . No extra config ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)
一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model
一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 package spittr.web; import static org.springframework.web.bind ...
随机推荐
- webform处理过程
一.post/get传值注意几点 post提交的时候,只有写了name属性且没有写disable=true表单元素(input,select,textarea)才会被提交. 如果不确定是get还是po ...
- sql server 2008 r2 清除数据库日志
USE [master] GO ALTER DATABASE [数据库名] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [数据库名] SET ...
- wamp优化
友情链接:IT狂人博客 转载请注明作者:浮沉雄鹰 和本文链接:http://www.cnblogs.com/xby1993/p/3342085.html 一.修改php.ini, 修改上传文件大小限制 ...
- vJine.Core 0.3.0.49 正式发布
nuget: https://www.nuget.org/packages/vJine.Core/ oschina: http://git.oschina.net/vjine/vJine.Core/a ...
- 解决VS2012【加载......符号缓慢】的问题
http://blog.csdn.net/shi0090/article/details/19411777 最近在用VS2012调试时,经常出现"加载......符号缓慢的问题", ...
- Chrome 建立SOCKS5代理
前提是putty已经在7090端口和远程服务器建立隧道. 假设你的chrome浏览器位于"C:\Program Files (x86)\Google\Chrome\Application\c ...
- 如何在Linux下创建与解压zip, tar, tar.gz和tar.bz2文件
这么多年来,数据压缩对我们来说是非常有用的.无论是在邮件中发送的图片用的zip文件还是在服务器压缩数据文件,我们都可以让下载更容易或者有效的节约磁盘空间.某些压缩格式有时允许我们以60%的比率(甚至更 ...
- 网站开发常用jQuery插件总结(一)提示插件alertify
1.alertify插件功能 主要实现提示功能,用于代替js中的alert,confirm,prompt,显示友好的提示框 2.alertify官方地址 http://fabien-d.github. ...
- [Linux]root默认密码问题
Linux/Ubuntu root默认密码问题 Ubuntu安装后root是没有默认的密码的,因为你还没给root设置密码,你第一个 user 是在 admin 组,他可以给 root 设置密码, ...
- oracle 11g实验五——触发器的使用
实验要求: 实验五 触发器的使用 实验目的 1. 理解触发器的概念.作用及分类: 2. 掌握触发器的创建.使用: 实验内容 1. 建立表orders:用于存储订单列表信息:表order_item ...