- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 WebApplicationInitializer
,我可以轻松地在 onStartup()
方法中向 ServletContext
添加过滤器。
如何使用WebMvcConfigurerAdapter
添加过滤器?我必须使用 XML 吗?
为了帮助大家更容易理解Spring Web Configuration,我画了下面的图。
现在您只需要先了解 Spring Web 配置背后的理性
即可。然后从下面选择要继承的配置类以及要重写的方法。
查找它比记住这么多事情要轻松得多。
还有一篇关于 Spring Web 初始化的好文章:
http://www.kubrynski.com/2014/01/understanding-spring-web-initialization.html
根据Tunaki
的回复,我检查了AbstractDispatcherServletInitializer
。过滤器注册发生在以下代码中:
即使我重写了绿色的 getServletFilters()
方法,我仍然无法访问 registerServletFilter()
的 Dyanmic
结果。那么如何通过addMappingForUrlPatterns()来配置过滤器呢?
看来我必须
重写整个registerDispatcherServlet()
方法。
最佳答案
WebMvcConfigurer
是一个接口(interface),用于为通过 @EnableWebMvc
启用的 Spring MVC 自定义基于 Java 的配置。 WebMvcConfigurerAdapter
只是一个为此接口(interface)提供默认空方法的适配器。
它不会配置使用过滤器的DispatcherServlet
。因此,您无法使用 WebMvcConfigurer
来配置 servlet 过滤器。
要轻松配置过滤器,您可以继承 AbstractDispatcherServletInitializer
并覆盖getServletFilters()
:
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new CharacterEncodingFilter() };
}
}
如果您想进一步配置过滤器,则必须覆盖 onStartup
相反:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addFilter("name", CharacterEncodingFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
}
关于spring - 如何在Spring中使用WebMvcConfigurerAdapter添加过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463918/
这是我正在处理的 WebConfig 代码: package hello.config; import org.springframework.context.annotation.Configura
我已经在 Spring MVC 中使用以下代码将 xml 转换为 Java 类。谁能告诉我如何设置 bean 创建的顺序。当我运行以下代码时。 JDBCTemplate bean 在 DataSour
我在我的 Spring Boot 应用程序中成功地使用了 @EnableSpringDataWebSupport 来启用分页、排序等等。然而,在某些时候,我不得不引入一个自定义参数解析器,并使用 Ja
我只是迁移到 spring mvc 版本 5.0.1.RELEASE 但突然在 eclipse STS WebMvcConfigurerAdapter 被标记为已弃用 public class Mvc
我正在尝试在 Spring Boot 应用程序中实现转换器,但由于某种原因,我对 addFormatters(FormatterRegistry formatterRegistry) 的重写从未被调用
注意以前已将其标记为已回答,但是随着我对基本组件的理解得到了改善,我看到我们的最初答案仅解决了空指针,但未解决根本原因。目前,根本原因似乎是缺少对分发侦听器和附加到容器的dwrServlet上上下文的
我想在 Windows 中使用 WebMvcConfigurerAdapter 添加资源处理程序,但在 Linux 中它不起作用,所以我添加了 WebMvcConfigurationSupport。
我想添加资源处理程序。在论坛中,他们使用 WebMvcConfigurationSupport:http://forum.springsource.org/showthread.php?116068-
由于 WebMvcConfigurerAdapter 从 Spring 5.0 开始已被弃用,我已将其更改为 WebMvcConfigurationSupport,但我得到的响应为空值。 { "key
我的应用有以下配置: @EnableTransactionManagement @EnableWebSecurity public class SecurityApiConfiguration e
有2个配置,在不同的jar文件中,我想控制拦截器注册的顺序。一个拦截器可能依赖于另一个拦截器设置的数据。 我已经在 addInterceptors 方法上尝试过 @Order。 @Configurat
我有一个使用 Spring Security 和 Thymeleaf 模板的 Spring Boot 应用程序。当 Controller 由 WebConfigurerAdapter 的子类管理时,我
我可以同时使用以下配置吗? 和 @Configuration @EnableWebMvc public class ResourceMappingsAdapter extends WebMvcCon
我想将两个配置合并为一个配置。这是它们目前的样子: WebMvcConfigurerAdapter @Configuration @EnableWebMvc @Import(...) public c
我有一个用于 web 的 Spring Boot hello world 和一些配置混淆: Spring 版本:1.2.6.RELEASE 我的项目结构: 我需要提供一些静态内容,所以我决定在一些自定
我是一名优秀的程序员,十分优秀!