WebApplicationInitializer

WebApplicationInitializer

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

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

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


我想念什么?我对使用注释从头开始创建Web应用程序有些陌生。

这是有问题的课程:

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


  返回值:
  FilterRegistration对象,可用于进一步配置给定的过滤器;如果此null已经包含具有给定的ServletContext的过滤器的完整FilterRegistration,或者如果已经具有相同的过滤器实例,则为filterName已在同一个容器中与此或另一个ServletContext注册


确保您没有已经注册此过滤器的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/*"};
    }

}

07-26 04:19