我正在使用maven创建一个基本的Web应用程序,然后将其导入到Eclipse 4.2中。我已经设置了Tomcat 7作为服务器。我尝试为Web应用程序配置Spring Data与MongoDB。
我正在遵循在此找到的基于代码的配置方法:WebApplicationInitializer。
当我在服务器上运行项目时,我在创建的WebApplicationInitializer类中得到了一个空指针异常。这行代码:container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 返回null。
我错过了什么?我有点新手,使用注释从头开始创建Web应用程序。
这是相关的类:
没有任何改变,仍然遇到NPE错误。我在这里读到(http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html),如果已经注册了一个servlet,那么container.addServlet将返回null?Tomcat是否已经注册了一个servlet?
很抱歉浪费了大家的时间,我还有一个web.xml文件也注册了相同的servlet,所以这个servlet只是返回null。现在开始修复404错误,可能是控制器出了问题。
我正在遵循在此找到的基于代码的配置方法: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("/*");
}
}
尝试将此添加到 POM 文件中:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
没有任何改变,仍然遇到NPE错误。我在这里读到(http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html),如果已经注册了一个servlet,那么container.addServlet将返回null?Tomcat是否已经注册了一个servlet?
很抱歉浪费了大家的时间,我还有一个web.xml文件也注册了相同的servlet,所以这个servlet只是返回null。现在开始修复404错误,可能是控制器出了问题。
dispatcher.setLoadOnStartup()
这一行上得到了一个空指针异常,考虑到前一行给出了一个空的dispatcher? - Biju Kunjummenweb.xml
文件,试图纯注释方式实现。 - Jagger