我试图为2个或3个控制器创建单个@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");
}
}
在上面我在2控制器中使用Save
所以我得到错误如果我使用单控制器它按预期工作
错误是:
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 [crm.venkat.controller.Clients@7e47dc12] 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 [crm.venkat.controller.ClientCompaniesController@7e47dc12] 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 :(得分: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");
}
}
现在你可以做的是点击一个网址/contacts/Save
,该网址会在contacts
类SaveContacts
方法中获得控制权,而/clients/Save
会转到client
class {SaveClients
方法。同样的更新。