spring mvc - Tuckey URL Rewrite Filter Java Class Configuration -
i have been researching how perform url rewrites on tomcat 8 , keep running same 2 suggestions.
1) use tuckey urlrewritefilter. 2) run apache on top of tomcat use mod_rewrite.
in regards former, urlrewritefilter doesn't appear have documentation how configure in java format opposed xml. spring mvc application not make use of web.xml file - configuration done via java classes - , i'm not in position configure xml.
is there way configure in way or there alternatives other trying run apache on top of tomcat?
for example, there way achieve in java opposed xml:
<filter> <filter-name>urlrewritefilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.urlrewritefilter</filter-class> </filter> <filter-mapping> <filter-name>urlrewritefilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>request</dispatcher> <dispatcher>forward</dispatcher> </filter-mapping>
this webapplicationinitializer class:
public class initializer implements webapplicationinitializer { @override public void onstartup(servletcontext container) { // create 'root' spring application context annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); rootcontext.register(rootconfig.class); // manage lifecycle of root application context container.addlistener(new contextloaderlistener(rootcontext)); // create dispatcher servlet's spring application context annotationconfigwebapplicationcontext dispatchercontext = new annotationconfigwebapplicationcontext(); dispatchercontext.register(webappconfig.class); // register , map dispatcher servlet servletregistration.dynamic dispatcher = container.addservlet("dispatcher", new dispatcherservlet(dispatchercontext)); dispatcher.setloadonstartup(1); dispatcher.addmapping("/*"); dispatcher.addmapping("*.html"); } }
my rootconfig
@configuration @componentscan(basepackages={"com.ucrisko.*"}, excludefilters={ @componentscan.filter(type=filtertype.annotation, value=enablewebmvc.class) }) public class rootconfig { }
and webappconfig
@configuration @enablewebmvc @componentscan(basepackages={"com.ucrisko.*"}) public class webappconfig extends webmvcconfigureradapter{ @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/resources/**").addresourcelocations("/resources/"); } ...various other beans }
in spring boot can define filter bean in configuration. i'm not sure if works in plain spring mvc can try. 1 awkward thing configuring filter java code - have implement filterconfig. working spring boot example looks (snippet config class):
@bean public urlrewritefilter urlrewritefilter(final servletcontext servletcontext) throws servletexception { urlrewritefilter urlrewritefilter = new urlrewritefilter(); urlrewritefilter.init(new filterconfig() { private final map<string, string> params = new hashmap<string, string>(); { params.put("confpath", "urlrewrite.xml"); } @override public string getfiltername() { return "urlreritefilter"; } @override public servletcontext getservletcontext() { return servletcontext; } @override public string getinitparameter(string name) { return params.get(name); } @override public enumeration<string> getinitparameternames() { return collections.enumeration(params.keyset()); } }); return urlrewritefilter; }
as regular spring mvc, seems should implement webapplicationinitializer
. webapplicationinitializer
java code equivalent of web.xml configuration. according documentation, spring should pick implementations classpath , run them. sample implementation may (note: haven't tested if works, should set on right path):
public class initalizer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return null; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[] {webappconfig.class}; } @override protected string[] getservletmappings() { return new string[] {"/*", "*.html"}; } @override protected filter[] getservletfilters() { urlrewritefilter urlrewritefilter = new urlrewritefilter(); /* * add filter configuration here if necessary */ return new filter[] {urlrewritefilter}; } }
abstractannotationconfigdispatcherservletinitializer
spring's implementation of webapplicationinitializer
interface intention make initialization of dispcherservlet
(which entry point spring mvc) easier. in example webappconfig.class
main spring java config class.
edit
if prefer keep current initializer code, should posssible add following lines in onstartup
method:
filterregistration.dynamic urlrewritefilter = servletcontext.addfilter("urlrewritefilter", new urlrewritefilter()); urlrewritefilter.setinitparameter("confpath", "urlrewrite.xml"); urlrewritefilter.addmappingforurlpatterns(enumset.of(dispatchertype.request, dispatchertype.forward), false, "/*");
this snipped based on answer available here: how filter mapping in abstractannotationconfigdispatcherservletinitializer spring
Comments
Post a Comment