java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name

后端 未结 2 486
无人及你
无人及你 2021-01-29 00:14

My initializer class

public class HomeServlet extends 
AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class         


        
2条回答
  •  一生所求
    2021-01-29 00:40

    1. use the configuration class as follows:

      @ComponentScan(basePackages={"spittr.controllers"})
      @Configuration
      @EnableWebMvc
      public class SpringContextConfig1 extends WebMvcConfigurerAdapter{
      
          @Override            
          public void configureViewResolvers(ViewResolverRegistry registry) {
              InternalResourceViewResolver ivr=new InternalResourceViewResolver();
              ivr.setPrefix("/WEB-INF/jsp/");
              ivr.setSuffix(".jsp");
              ivr.setExposeContextBeansAsAttributes(true);
              registry.viewResolver(ivr);
          }
      }
      

      Basically you're extending WebMvcConfigurerAdapter without inheriting any of its methods (in my 4.3.3 Spring version al least).

    2. since you have a single DispatcherServlet here, you can add the SpringContextConfig1 class to the root context and leave the servlet context empty: switch the body of the method getServletConfigClasses() under the getRootConfigClasses() and vice versa - see A Bit on ApplicationContext Hierarchies.


    Moreover, the DispatcherServlet mapping is more likely / instead of /home:

    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
    

提交回复
热议问题