小编Mat*_*ttG的帖子

WebApplicationInitializer container.addServlet()返回null

我正在使用maven创建一个基本的Web应用程序,然后导入到Eclipse 4.2.我将Tomcat 7设置为服务器.我正在尝试使用mongodb为Web应用程序配置spring数据.

我遵循这里找到的基于代码的配置方法:WebApplicationInitializer

当我在服务器上运行项目时,我在我创建的WebApplicationInitializer类中得到一个空指针异常.行:container.addServlet("dispatcher",new DispatcherServlet(dispatcherContext)); 返回null.

我错过了什么?我使用注释从头开始创建Web应用程序有点新鲜.

这是有问题的课程:

public class ATWWebAppInitializer implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext container) throws ServletException  
    {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new  AnnotationConfigWebApplicationContext();
      rootContext.register(SpringMongoConfig.class);

      // Manage the lifecycle of the root application context
      container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(ATWDispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/*");
    }
} 
Run Code Online (Sandbox Code Playgroud)

尝试将此添加到POM: …

eclipse web-applications spring-data

7
推荐指数
1
解决办法
2486
查看次数

标签 统计

eclipse ×1

spring-data ×1

web-applications ×1