Spring Boot - 为什么使用 Tiles+JSP 时在单个请求中多次调用 MyFilter?
Spring Boot - Why is MyFilter called many times in a single request when using Tiles+JSP?
给定此过滤器:
public class MyFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("FILTERING!!!!!!!!!!!!!!!!!!!"+((HttpServletRequest)request).getRequestURI());
chain.doFilter(request, response);
}
我看到它在同一个请求中被多次调用
Spring 启动应用程序:
@SpringBootApplication(scanBasePackageClasses={MySpringBootDemoApplication .class, TilesConfiguration.class})
public class MySpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootDemoApplication .class, args);
}
@Bean
public Filter myFilter(){
return new MyFilter();
}
}
瓷砖配置:
@Configuration
public class TilesConfiguration {
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] { "WEB-INF/**/tiles.xml" });
configurer.setCheckRefresh(true);
return configurer;
}
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
}
这是我在日志中看到的:
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
浏览器发出的唯一真实请求是:
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home
与(/standard.jsp
)相关的图块从何而来?在传统的 Spring MVC 应用程序中,它们从未出现过。
我知道 MyFilter 可以扩展 OncePerRequestFilter,但我无法修改我必须使用的真实过滤器,它们来自遗留库。
我刚刚意识到,由于 MyFilter 是针对 /*
映射的,它也会在容器转发时被调用(因此 Tile 的 standard.jsp...)。我更改了 DispatcherServlet 以匹配 /action/*
,过滤器也是如此:
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean frb = new FilterRegistrationBean();
frb.setFilter(new MyFilter());
frb.setUrlPatterns(Arrays.asList(new String[]{"/action/*"}));
return frb;
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("/action/*");
return registration;
}
给定此过滤器:
public class MyFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("FILTERING!!!!!!!!!!!!!!!!!!!"+((HttpServletRequest)request).getRequestURI());
chain.doFilter(request, response);
}
我看到它在同一个请求中被多次调用
Spring 启动应用程序:
@SpringBootApplication(scanBasePackageClasses={MySpringBootDemoApplication .class, TilesConfiguration.class})
public class MySpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootDemoApplication .class, args);
}
@Bean
public Filter myFilter(){
return new MyFilter();
}
}
瓷砖配置:
@Configuration
public class TilesConfiguration {
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] { "WEB-INF/**/tiles.xml" });
configurer.setCheckRefresh(true);
return configurer;
}
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
}
这是我在日志中看到的:
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
浏览器发出的唯一真实请求是:
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home
与(/standard.jsp
)相关的图块从何而来?在传统的 Spring MVC 应用程序中,它们从未出现过。
我知道 MyFilter 可以扩展 OncePerRequestFilter,但我无法修改我必须使用的真实过滤器,它们来自遗留库。
我刚刚意识到,由于 MyFilter 是针对 /*
映射的,它也会在容器转发时被调用(因此 Tile 的 standard.jsp...)。我更改了 DispatcherServlet 以匹配 /action/*
,过滤器也是如此:
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean frb = new FilterRegistrationBean();
frb.setFilter(new MyFilter());
frb.setUrlPatterns(Arrays.asList(new String[]{"/action/*"}));
return frb;
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("/action/*");
return registration;
}