2014-12-27 90 views
0

我試圖創建2個或3控制器不幸的是我收到提示單@RequestMapping值單@RequestMapping多個控制器

如:

@Controller 
public class contatcs { 

    @RequestMapping(value="Save", params="m=contacts") 
    public ModelAndView SaveContacts(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/savecontacts"); 
    } 

    @RequestMapping(value="update", params="m=contacts") 
    public ModelAndView UpdateContacts(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/updatecontacts"); 
    } 
} 



@Controller 
public class clients { 

    @RequestMapping(value="Save", params="m=clients") 
    public ModelAndView SaveClients(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/saveclients"); 
    } 

    @RequestMapping(value="update", params="m=clients") 
    public ModelAndView updateClients(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/updateclients"); 
    } 
} 
以上

我用Save 2控制器如果我單控制器使用,因此我收到提示它的工作原理如我所料

錯誤是:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler [clients] to URL path [//save]: There is already handler [[email protected]] mapped. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IllegalStateException: Cannot map handler [clientCompaniesController] to URL path [//Search]: There is already handler [[email protected]] mapped. 
    at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:376) 
    at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:348) 
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82) 
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58) 
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119) 
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:71) 
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73) 
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:99) 
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:82) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1396) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
    ... 20 more 
+0

它說在這裏»無法映射處理程序[clientCompaniesController]到URL路徑[/ /搜索]:已經處理程序«原因所在。 –

回答

3

你不能對各種控制器相同url-mapping但是你可以做的是:

@Controller 
@RequestMapping("/contacts") //line added here 
public class contatcs { 

    @RequestMapping(value="/Save", params="m=contacts") 
    public ModelAndView SaveContacts(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/savecontacts"); 
    } 

    @RequestMapping(value="/update", params="m=contacts") 
    public ModelAndView UpdateContacts(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/updatecontacts"); 
    } 
} 



@Controller 
@RequestMapping("/clients") //line added here 
public class clients { 

    @RequestMapping(value="/Save", params="m=clients") 
    public ModelAndView SaveClients(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/saveclients"); 
    } 

    @RequestMapping(value="/update", params="m=clients") 
    public ModelAndView updateClients(@ModelAttribute("command") ContactsBean contactsBean, 
     BindingResult result) 
    //some stuff 
    return new ModelAndView("/updateclients"); 
    } 
} 

現在你可以做的是火一個url /contacts/Save將得到控制contacts類的SaveContacts方法和/clients/Save會去到client類'SaveClients方法。和更新類似。