web.xml 中以编码方式添加filter并设置初始化参数
来源:互联网 发布:在男人眼里的丑女知乎 编辑:程序博客网 时间:2024/05/22 10:49
web.xml中配置filter
<?xml version="1.0" encoding="UTF-8"?><web-app version="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></web-app>
假如有以下场景:
testFilet的初始化参数specialUrl在开发环境,测试环境和生产环境中有所区别,那么在系统提交测试,或者上线前必须要先修改web.xml,这样的话就会比较繁琐,给系统升级或测试增加额外开销。
这样就出现一个需求,希望能在系统启动时读取不同环境的配置信息来初始化web.xml,这个配置信息可以是库中的数据,或者固定的配置文件,最好这些配置信息能够一劳永逸,兼容开发环境,测试环境,生产环境,那以后就省事了。有一个开关去控制是什么环境,那就读取相应环境的配置数据。
编码实现web.xml配置filter
那在这样的需求下,web.xml就需要以编码的方式来实现配置。spring4.0以上的版本支持web.xml的编码配置。实现AbstractAnnotationConfigDispatcherServletInitializer接口,在servlet3.0中web.xml启动时会检测该接口实现类,从能够在实现类中去配置filter。
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;public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return null; } @Override public void onStartup(ServletContext servletContext) throws ServletException { // 系统启动时注册filter FilterRegistration testFilter = servletContext.addFilter("testFilter", TestFilter.class); // 设置init param, param可以从properties文件中读取或其他方式获取,提供一个想法 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); }}
推荐一篇相同类型的博客:xml零配置之AbstractAnnotationConfigDispatcherServletInitializer
需要注意的是以上的实现,依赖servlet-api-3.0.jar和spring-webmvc-4.0以上版本jar包。
0 0
- web.xml 中以编码方式添加filter并设置初始化参数
- web.xml设置编码格式---在filter 过滤并设置当前要设置的编码格式
- web.xml中配置,获取初始化参数
- 在Filter中获取web.xml中配置的参数
- 在JSP页面中访问web.xml中的初始化参数
- application 从web.xml中获取初始化参数
- servlet获取web.xml中配置的初始化参数
- Spring在web.xml请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
- web.xml配置编码方式
- 以Windows服务方式启动MySQL,并将其默认编码设置为UTF-8
- 以Windows服务方式启动MySQL,并将其默认编码设置为UTF-8
- 由于web.xml中字符编码的Filter的位置不对导致的问题
- 编写Filter时 web.xml内有多条初始化信息取出方法
- Web.xml中设置Servlet和Filter时的url-pattern匹配规则
- web.xml中filter的用法
- web.xml中filter的用法
- web.xml中过滤器filter的用法
- web.xml中filter的用法
- 阿里巴巴集团CRO刘振飞:双11的六维技术准备
- 下标运算符[]重载
- 虚拟机VirtualBox安装遇见问题
- 欢迎使用CSDN-markdown编辑器
- python分别求个十百千位
- web.xml 中以编码方式添加filter并设置初始化参数
- 解一元多次方程
- Spring简介
- <javascript>元素更新后不立即显示在页面上的问题
- LightOJ - 1374 Confusion in the Problemset (模拟)
- VMware vCenter服务器端口-VMware ESXi和ESX主机
- Linux磁盘分区与挂载
- Java 内部类的一些总结
- android.view.WindowManager$BadTokenException: Unable to add window — token null