Config filter in Web. xml
<?xml version= "1.0" encoding= "UTF-8"?><Web-appversion="2.5"xmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" ><Display-name></Display-name><Filter><Filter-name>testfilter</Filter-name><Filter-class>com.bolin.core.testfilter.java</Filter-class><Init-param><Param-name>specialurl</Param-name><Param-value>/login,/register</Param-value></Init-param></filter> <filter-mapping> <filter-name>testfilter</filter-name> <url-pattern>*.*</url-pattern> </ filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>< Span class= "Hljs-tag" ></WEB-APP>
If you have the following scenarios:
Testfilet initialization Parameters Specialurl in the development environment, test environment and production environment is different, then the system to submit tests, or go online before the need to modify Web. XML, so it will be more cumbersome, to the system upgrade or testing to add additional costs.
So there is a need to be able to read the configuration information of different environment at the start of the system to initialize the Web. XML, this configuration information can be the data in the library, or a fixed configuration file, preferably these configuration information can once and for all, compatible with the development environment, test environment, production environment, then it will be convenient. There is a switch to control what environment, then read the configuration data of the corresponding environment.
encoding implements the Web. XML Configuration Filter
That being the case, Web. XML needs to be coded to implement the configuration. Versions above spring4.0 support the encoding configuration of Web. Xml. Implements the Abstractannotationconfigdispatcherservletinitializer interface, which detects the interface implementation class when Web. XML is started in servlet3.0, from the ability to configure filter in the implementation class.
Package Com.bolin.core;Import java.io.IOException;Import Java.util.EnumSet;Import Java.util.Iterator;Import java.util.Properties;Import Javax.servlet.DispatcherType;Import Javax.servlet.Filter;Import javax.servlet.FilterRegistration;Import javax.servlet.FilterRegistration.Dynamic;Import Javax.servlet.ServletContext;Import javax.servlet.ServletException;Import Org.apache.commons.lang.StringUtils;Import Org.springframework.core.io.ClassPathResource;Import Org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;PublicClassWebappinitializerExtendsabstractannotationconfigdispatcherservletinitializer{@OverrideProtected class<?>[]Getrootconfigclasses () {ReturnNull }@OverrideProtected class<?>[]Getservletconfigclasses () {ReturnNull }@OverrideProtected string[]Getservletmappings () {ReturnNull }@OverridePublicvoidOnstartup (ServletContext ServletContext)throws servletexception {//Register filter Filterregistration when system starts Testfilter = Servletcontext.addfilter (//set init param, param can be read from the properties file or otherwise obtained, providing an idea testfilter.setinitparameter (" Specialurl ", "/login,/register "); Testfilter.addmappingforurlpatterns (Enumset.allof (dispatchertype.class), true, "* *"); super.onstartup (ServletContext);} @Override protected Dynamic Registerservletfilter (ServletContext arg0, filter filter) {return super.registerservletfilter (arg0, Filter),}}
Recommend a blog of the same type: XML 0 configuration Abstractannotationconfigdispatcherservletinitializer
It is important to note that the above implementations rely on Servlet-api-3.0.jar and spring-webmvc-4.0 versions of the jar package.
Add filter in Web. XML and set initialization parameters Abstractannotationconfigdispatchservletinitializer