how to add filters to servlet when using @enablewebmvc annotation in spring 4? -
here current configuration
public class webappconfig implements webapplicationinitializer { private static final string character_encoding_filter_encoding = "utf-8"; private static final string character_encoding_filter_name = "characterencoding"; private static final string character_encoding_filter_url_pattern = "/*"; private static final string dispatcher_servlet_name = "dispatcher"; private static final string dispatcher_servlet_mapping = "/"; @override public void onstartup(servletcontext servletcontext) throws servletexception { annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); rootcontext.register(exampleapplicationcontext.class); configuredispatcherservlet(servletcontext, rootcontext); enumset<dispatchertype> dispatchertypes = enumset.of(dispatchertype.request, dispatchertype.forward); configurecharacterencodingfilter(servletcontext, dispatchertypes); servletcontext.addlistener(new contextloaderlistener(rootcontext)); } private void configuredispatcherservlet(servletcontext servletcontext, webapplicationcontext rootcontext) { servletregistration.dynamic dispatcher = servletcontext.addservlet( dispatcher_servlet_name, new dispatcherservlet(rootcontext) ); dispatcher.setloadonstartup(1); dispatcher.addmapping(dispatcher_servlet_mapping); } private void configurecharacterencodingfilter(servletcontext servletcontext, enumset<dispatchertype> dispatchertypes) { characterencodingfilter characterencodingfilter = new characterencodingfilter(); characterencodingfilter.setencoding(character_encoding_filter_encoding); characterencodingfilter.setforceencoding(true); filterregistration.dynamic characterencoding = servletcontext.addfilter(character_encoding_filter_name, characterencodingfilter); characterencoding.addmappingforurlpatterns(dispatchertypes, true, character_encoding_filter_url_pattern); } }
now want use @enablewebmvc
@enablewebmvc @componentscan(basepackages = { "com.example.mvc.base.controller" }) public class webappconfig extends webmvcconfigureradapter { }
how add filters
, listeners
servletcontext did using webapplicationinitializer
?
- you still continue adding filters , listeners thorough webapplicationinitializer. class methods starts web application servlet registered required contexts , beans.
- in class dispatcherservlet created containing programmatically configured annotationconfigwebapplicationcontext.the dispatcher mapped .html.
- webappconfig class enables webmvc based on annotation , scans base packages annotated resources. these further registered web application context of default servlet initialized webapplicationinitializer.
Comments
Post a Comment