WebApplicationInitializer container.addFilter() returns 空
WebApplicationInitializer container.addFilter() returns null
我正在创建一个基于 REST
的 Web 应用程序,其中 AngularJS
作为前端,基于 REST
的后端(使用 Spring 4
)。我遵循此处找到的基于代码的配置方法:WebApplicationInitializer
当我 运行 服务器上的项目时,我在行中得到一个空值:
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
我错过了什么?我对使用注释从头开始创建 Web 应用程序有点陌生。
这里是有问题的class:
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");
}
这一行给我 null
com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
我试过了,结果一样
FilterRegistration.Dynamic filter1 = container.addFilter("prerender", com.github.greengerong.PreRenderSEOFilter.class);
当方法 addFilter
returns 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/*"};
}
}
我通过 运行 maven 命令 mvn clean
解决了它,我不知道某处有问题并给我带来了奇怪的行为。我不确定我从这个问题中学到了什么:(
感谢您的帮助。
我正在创建一个基于 REST
的 Web 应用程序,其中 AngularJS
作为前端,基于 REST
的后端(使用 Spring 4
)。我遵循此处找到的基于代码的配置方法:WebApplicationInitializer
当我 运行 服务器上的项目时,我在行中得到一个空值:
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
我错过了什么?我对使用注释从头开始创建 Web 应用程序有点陌生。
这里是有问题的class:
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");
}
这一行给我 null
com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
我试过了,结果一样
FilterRegistration.Dynamic filter1 = container.addFilter("prerender", com.github.greengerong.PreRenderSEOFilter.class);
当方法 addFilter
returns null
时,表示该名称已经存在 a filter registered。
Returns:
aFilterRegistration
object that may be used to further configure the given filter, ornull
if thisServletContext
already contains a completeFilterRegistration
for a filter with the givenfilterName
or if the same filter instance has already been registered with this or anotherServletContext
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/*"};
}
}
我通过 运行 maven 命令 mvn clean
解决了它,我不知道某处有问题并给我带来了奇怪的行为。我不确定我从这个问题中学到了什么:(
感谢您的帮助。