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

后端 未结 2 487
无人及你
无人及你 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[] {"/"};
    }
    
    0 讨论(0)
  • 2021-01-29 01:01

    My friend encountered the same problem. The problem was that the correct package name was not set for the class. AAAAnd clean the project before packaging!!!

    Here is the code on GitHub

    0 讨论(0)
提交回复
热议问题