有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

部署servlet时spring“java.lang.IllegalStateException:无ServletContext集”

我将在tomcat上部署spring mvc webapp WAR包。部署过程失败,出现以下错误:“java。lang.IllegalStateException:没有ServletContext集'

我猜我的配置有问题:(

我的webapp初始值设定项:

package com.jbtits.spring.mvc.webac;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();

        DispatcherServlet servlet = new DispatcherServlet(applicationContext);
        ServletRegistration.Dynamic registration = servletContext.addServlet("webac", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}

我的webapp配置:

package com.jbtits.spring.mvc.webac;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@EnableWebMvc 
@ComponentScan("com.jbtits.spring.mvc.webac")
public class AppConfig extends WebMvcConfigurationSupport {
}

就这样,只有两颗豆子

Tomcat故障输出:

02-Oct-2019 18:02:52.971 WARNING [http-nio-8081-exec-84] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set


共 (0) 个答案