无法注册名称为“ dispatcher”的servlet。请检查是否存在另一个使用相同名称注册的servlet。

时间:2018-09-01 04:29:54

标签: java maven spring-mvc

下面的Java代码有什么问题,这是什么意思?谁能帮我解决这个问题:

  

java.lang.IllegalArgumentException:无法注册名称为'dispatcher'的servlet。检查是否有另一个servlet注册了相同的名称。

AppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.kk.kk.config")
public class AppConfig {
//Define a bean for ViewResolver

@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/view/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
  }

}

DispatcherServletInitializer.java

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
    //TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses(){
        return new Class[] {AppConfig.class};
    }

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

}

AppController.java

@Controller
public class AppController {

    @GetMapping("/")
    public String showHome(){

        return "home";
    }

}

1 个答案:

答案 0 :(得分:0)

您应该从WebMvcConfigurerAdapter开始:

尝试一下:

@Configuration
@ComponentScan(basePackages="com.kk.kk.config")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter{
    @Override            
    public void configureViewResolvers(ViewResolverRegistry registry)
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            registry.viewResolver(resolver);
    }
}