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

时间:2016-12-13 14:51:30

标签: spring spring-mvc illegalargumentexception spring-java-config servlet-mapping

我的初始化程序类

public class HomeServlet extends 
AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {

    return null;
}

@Override
protected Class<?>[] getServletConfigClasses() {

    return new Class<?>[]{SpringContextConfig1.class};
}

@Override
protected String[] getServletMappings() {

    return new String[] {"/home"};
}

}

配置类

@ComponentScan(basePackages={"spittr.controllers"})
@Configuration
@EnableWebMvc
public class SpringContextConfig1 extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver ivr=new InternalResourceViewResolver();
    ivr.setPrefix("/WEB-INF/jsp/");
    ivr.setSuffix(".jsp");
    ivr.setExposeContextBeansAsAttributes(true);
    return ivr;
}

}

控制器

@Controller
public class HomeController {


@RequestMapping(value="/home",method=RequestMethod.GET)
public String home(){
    return "home";
}

}

这是一个非常简单的程序,我写的是测试Spring MVC的JavaConfig。我完全按照“Spring in Action”一书中的所有步骤进行操作。

当我运行此代码时,我收到此错误

  

09:41:37,854 ERROR [org.jboss.msc.service.fail](ServerService线程池 - 72)MSC000001:无法启动服务jboss.undertow.deployment.default-server.default-host./spittr :服务中的org.jboss.msc.service.StartException:jboss.undertow.deployment.default-server.default-host。/ spittr:java.lang.RuntimeException:java.lang.IllegalArgumentException:无法注册名为“dispatcher”的servlet 。检查是否有另一个以相同名称注册的servlet。       在org.wildfly.extension.undertow.deployment.UndertowDeploymentService $ 1.run(UndertowDeploymentService.java:85)       at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)       在org.jboss.threads.JBossThread.run(JBossThread.java:320)   引起:java.lang.RuntimeException:java.lang.IllegalArgumentException:无法注册名为'dispatcher'的servlet。如果有另一个servlet注册在同一名称下,请检查。       在io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:236)       在org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)       在org.wildfly.extension.undertow.deployment.UndertowDeploymentService $ 1.run(UndertowDeploymentService.java:82)       ......还有6个   引起:java.lang.IllegalArgumentException:无法注册名为'dispatcher'的servlet。如果有另一个servlet注册在同一名称下,请检查。       在org.springframework.util.Assert.notNull(Assert.java:115)       在org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98)       在org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:71)       在org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)       at io.undertow.servlet.core.DeploymentManagerImpl $ 1.call(DeploymentManagerImpl.java:186)       在io.undertow.servlet.core.DeploymentManagerImpl $ 1.call(DeploymentManagerImpl.java:171)       在io.undertow.servlet.core.ServletRequestContextThreadSetupAction $ 1.call(ServletRequestContextThreadSetupAction.java:42)       at io.undertow.servlet.core.ContextClassLoaderSetupAction $ 1.call(ContextClassLoaderSetupAction.java:43)       at io.undertow.servlet.api.LegacyThreadSetupActionWrapper $ 1.call(LegacyThreadSetupActionWrapper.java:44)       at io.undertow.servlet.api.LegacyThreadSetupActionWrapper $ 1.call(LegacyThreadSetupActionWrapper.java:44)       at io.undertow.servlet.api.LegacyThreadSetupActionWrapper $ 1.call(LegacyThreadSetupActionWrapper.java:44)       at io.undertow.servlet.api.LegacyThreadSetupActionWrapper $ 1.call(LegacyThreadSetupActionWrapper.java:44)       at io.undertow.servlet.api.LegacyThreadSetupActionWrapper $ 1.call(LegacyThreadSetupActionWrapper.java:44)       在io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)

错误的高亮是无法使用名称'dispatcher'注册servlet。检查是否有另一个以相同名称注册的servlet。在org.wildfly.extension。 undertow.deployment.UndertowDeploymentService $ 1.run(UndertowDeploymentService.java:85)

请帮我解决这个问题。我在日食上使用WildFly-10。

2 个答案:

答案 0 :(得分:0)

  1. 使用配置类,如下所示:

    @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);
        }
    }
    

    基本上你在没有继承任何方法的情况下扩展WebMvcConfigurerAdapter(至少在我的4.3.3 Spring版本中)。

  2. 因为这里只有一个DispatcherServlet,所以可以将SpringContextConfig1类添加到根上下文中,并将servlet上下文留空:切换方法的主体getServletConfigClasses()getRootConfigClasses()反之亦然 - 请参阅A Bit on ApplicationContext Hierarchies

  3. 此外,DispatcherServlet映射更有可能/而不是/home

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

答案 1 :(得分:0)

我的朋友遇到了同样的问题。 问题是没有为类设置正确的包名。 AAA并在包装前清洁项目!!!

以下是GitHub

上的代码