title: Mapping a single controller to several beans in spring
body: <p>I need to create several beans for my static pages with different IDs using a single controller class which has an attribute to define a jsp name to be displayed.</p>
  
  <p>I used to do this with an older version using FormControllers but I cannot in version 3.2.4
  I checked several topics and understand that spring beans are singletons but
  I want to know if this is possible or if there is a work-around without creating empty controller classes for each page.</p>
  
  <p>Here is the BaseController class:</p>
  
  <pre><code>    public class BaseController extends AbstractController {
  
      public String contentPage;
  
      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
          ModelAndView mv = new ModelAndView("common/mainlayout");
  
          if(contentPage != null &amp;&amp; !contentPage.equals("")){
              mv.addObject("mainContent", "maincontent/" + contentPage);
              mv.addObject("mainMenuSelect", contentPage);
          }
  
          return mv;
      }
  
      public String getContentPage() {
          return contentPage;
      }
      public void setContentPage(String contentPage) {
          this.contentPage = contentPage;
      }
  }
  </code></pre>
  
  <p>Here is my mapping in spring-servlet.xml Note that I also tried defining class of each bean using my BaseController</p>
  
  <pre><code>&lt;bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
      &lt;property name="mappings"&gt;
          &lt;props&gt;
  
              &lt;!-- STATIC PAGES --&gt;
              &lt;prop key="/index.dlg"&gt;indexPageController&lt;/prop&gt;
              &lt;prop key="/faq.dlg"&gt;faqController&lt;/prop&gt;
              &lt;prop key="/howto.dlg"&gt;howtoController&lt;/prop&gt;
              &lt;prop key="/contactUs.dlg"&gt;contactUsController&lt;/prop&gt;
              &lt;prop key="/aboutUs.dlg"&gt;aboutUsController&lt;/prop&gt;
  
              &lt;!-- DYNAMIC PAGES --&gt;
              &lt;prop key="/postBox.dlg"&gt;postBoxController&lt;/prop&gt;
              &lt;prop key="/invoiceBox.dlg"&gt;invoiceController&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
      &lt;property name="interceptors"&gt;
          &lt;list&gt;
              &lt;ref bean="siteInterceptor" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="siteInterceptor" class="com.entegrator.spring.interceptor.SiteInterceptor"&gt;&lt;/bean&gt;
  
  &lt;!-- STATIC PAGE CONTROLLERS --&gt;
  &lt;bean id="baseController" abstract="true" class="com.entegrator.spring.controller.BaseController" /&gt;
  
  &lt;bean id="indexPageController" parent="baseController" &gt;
      &lt;property name="contentPage" value="index" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="faqController" parent="baseController" &gt;
      &lt;property name="contentPage" value="faq" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="howtoController" parent="baseController" &gt;
      &lt;property name="contentPage" value="howto" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="contactUsController" parent="baseController" &gt;
      &lt;property name="contentPage" value="contactUs" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="aboutUsController" parent="baseController" &gt;
      &lt;property name="contentPage" value="aboutUs" /&gt;
  &lt;/bean&gt;
  &lt;!-- STATIC PAGE CONTROLLERS END --&gt;
  
  
  &lt;!-- DYNAMIC PAGE CONTROLLERS --&gt;
  &lt;bean id="postBoxController" class="com.entegrator.spring.controller.PostBoxController"&gt;
      &lt;property name="postBoxManager" ref="postBoxManager" /&gt;
  &lt;/bean&gt;
  &lt;bean id="invoiceController" class="com.entegrator.spring.controller.InvoiceController"&gt;
      &lt;property name="postBoxManager" ref="postBoxManager" /&gt;
  &lt;/bean&gt;
  &lt;!-- DYNAMIC PAGE CONTROLLERS END --&gt;
  </code></pre>
  
  <p>my web.xml is:</p>
  
  <pre><code>&lt;display-name&gt;EntegratorPortal&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.dlg&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Here is my error log:</p>
  
  <pre><code> SEVERE: StandardWrapper.Throwable
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'faqController' to URL path [/base*]: There is already handler 'indexPageController' mapped.
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      at javax.servlet.GenericServlet.init(GenericServlet.java:160)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: java.lang.IllegalStateException: Cannot map handler 'faqController' to URL path [/base*]: There is already handler 'indexPageController' mapped.
      at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:294)
      at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:266)
      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:72)
      at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
      at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:117)
      at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:92)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1475)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
      ... 32 more
  </code></pre>
  
  <p>Thanks for your help</p>