我需要使用单个控制器类为具有不同 ID 的静态页面创建几个 bean,该控制器类具有定义要显示的 jsp 名称的属性。
我曾经使用 FormControllers 对旧版本执行此操作,但在 3.2.4 版本中无法做到这一点每个页面的控制器类。
这是 BaseController 类:
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 && !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;
}
}
这是我在 spring-servlet.xml 中的映射注意,我还尝试使用我的 BaseController 定义每个 bean 的类
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- STATIC PAGES -->
<prop key="/index.dlg">indexPageController</prop>
<prop key="/faq.dlg">faqController</prop>
<prop key="/howto.dlg">howtoController</prop>
<prop key="/contactUs.dlg">contactUsController</prop>
<prop key="/aboutUs.dlg">aboutUsController</prop>
<!-- DYNAMIC PAGES -->
<prop key="/postBox.dlg">postBoxController</prop>
<prop key="/invoiceBox.dlg">invoiceController</prop>
</props>
</property>
<property name="interceptors">
<list>
<ref bean="siteInterceptor" />
</list>
</property>
</bean>
<bean id="siteInterceptor" class="com.entegrator.spring.interceptor.SiteInterceptor"></bean>
<!-- STATIC PAGE CONTROLLERS -->
<bean id="baseController" abstract="true" class="com.entegrator.spring.controller.BaseController" />
<bean id="indexPageController" parent="baseController" >
<property name="contentPage" value="index" />
</bean>
<bean id="faqController" parent="baseController" >
<property name="contentPage" value="faq" />
</bean>
<bean id="howtoController" parent="baseController" >
<property name="contentPage" value="howto" />
</bean>
<bean id="contactUsController" parent="baseController" >
<property name="contentPage" value="contactUs" />
</bean>
<bean id="aboutUsController" parent="baseController" >
<property name="contentPage" value="aboutUs" />
</bean>
<!-- STATIC PAGE CONTROLLERS END -->
<!-- DYNAMIC PAGE CONTROLLERS -->
<bean id="postBoxController" class="com.entegrator.spring.controller.PostBoxController">
<property name="postBoxManager" ref="postBoxManager" />
</bean>
<bean id="invoiceController" class="com.entegrator.spring.controller.InvoiceController">
<property name="postBoxManager" ref="postBoxManager" />
</bean>
<!-- DYNAMIC PAGE CONTROLLERS END -->
我的 web.xml 是:
<display-name>EntegratorPortal</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.dlg</url-pattern>
</servlet-mapping>
这是我的错误日志:
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
谢谢你的帮助