How to add a Filter with WebMvcConfigurerAdapter in Spring? -
with webapplicationinitializer
, can add filter servletcontext
within onstartup()
method.
how add filter webmvcconfigureradapter
? have use xml?
add 1
to others understand spring web configuration more easily, draw following illustration.
now need first understand rational
behind spring web configuration. , pick config class inherit , method override below.
it's less painful remember many things.
and article on spring web initialization:
http://www.kubrynski.com/2014/01/understanding-spring-web-initialization.html
add 2
based on tunaki
's reply, checked abstractdispatcherservletinitializer
. filter registration happens in following code:
even override green getservletfilters()
method, still cannot access dyanmic
result of registerservletfilter()
. how can configure filter addmappingforurlpatterns()
?
it seems have to
override whole registerdispatcherservlet()
method.
webmvcconfigurer
interface used customize java-based configuration spring mvc enabled via @enablewebmvc
. webmvcconfigureradapter
adapter providing default empty methods interface.
it not configure dispatcherservlet
, filters used by. such, can't use webmvcconfigurer
configure servlet filters.
to configure filters, can inherit abstractdispatcherservletinitializer
, override getservletfilters()
:
public class mywebappinitializer extends abstractdispatcherservletinitializer { @override protected filter[] getservletfilters() { return new filter[] { new characterencodingfilter() }; } }
if want further configure filter, have override onstartup
instead:
@override public void onstartup(servletcontext servletcontext) throws servletexception { super.onstartup(servletcontext); servletcontext.addfilter("name", characterencodingfilter.class) .addmappingforurlpatterns(null, false, "/*"); }
Comments
Post a Comment