Add filter in Web. XML and set initialization parameters Abstractannotationconfigdispatchservletinitializer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.