gpt4 book ai didi

java - WebApplicationInitializer container.addFilter() 返回 null

转载 作者:行者123 更新时间:2023-12-01 22:14:31 24 4
gpt4 key购买 nike

我正在创建一个基于 REST 的 Web 应用程序,以 AngularJS 作为前端,以 REST 为基础的后端(使用 Spring 4 )。我遵循此处找到的基于代码的配置方法:WebApplicationInitializer

当我在服务器上运行该项目时,我在该行中得到一个空值:

FilterRegistration.Dynamic filter =  container.addFilter("prerender", seoFilter);

我错过了什么?我对使用注释从头开始创建网络应用程序有点陌生。

这是有问题的类:

public class MyWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {

XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:MyContext.xml");

ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/api/*");

com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
filter.setInitParameter("prerenderToken", "123456789123456789");
filter.addMappingForUrlPatterns(null , true, "/*");

ServletRegistration.Dynamic initSysConfiguration
= container.addServlet("initSysConfiguration", new InitSystemConfigurations());
initSysConfiguration.setLoadOnStartup(1);
initSysConfiguration.addMapping("/InitSystemConfigurations");

}

这一行给我空

com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();

我尝试过,但结果相同

FilterRegistration.Dynamic filter1 =  container.addFilter("prerender",  com.github.greengerong.PreRenderSEOFilter.class);

最佳答案

当方法 addFilter 返回 null 时,意味着该名称已经存在 a filter registered

Returns:
a FilterRegistration object that may be used to further configure the given filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName or if the same filter instance has already been registered with this or another ServletContext in the same container

确保您没有已注册此过滤器的 web.ml

另一个技巧是不要自己实现WebApplicationInitializer,而是扩展AbstractDispatcherServletInitializer并实现所需的方法。

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);

com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
filter.setInitParameter("prerenderToken", "123456789123456789");
filter.addMappingForUrlPatterns(null , true, "/*");

ServletRegistration.Dynamic initSysConfiguration
= container.addServlet("initSysConfiguration", new InitSystemConfigurations());
initSysConfiguration.setLoadOnStartup(1);
initSysConfiguration.addMapping("/InitSystemConfigurations");

}

protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:MyContext.xml");
return appContext;
}

protected String[] getServletMappings() {
return new String[] {"/api/*"};
}

}

关于java - WebApplicationInitializer container.addFilter() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com