我需要使用单个控制器类为具有不同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
谢谢你的帮助
最佳答案
您是否可以简单地在每个页面的一个控制器中使用@RequestMapping(value =“ / foo” method =“ RequestMethod.GET”)注释的方法?
这似乎是最简单的解决方案。
您的控制器看起来像这样:
@Controller
public class BaseController
{
@RequestMapping(value="/index", method="RequestMethod.GET")
public String returnIndexPage()
{
return "Your Index JSP Page Location Here";
}
@RequestMapping(value="/faq", method="RequestMethod.GET")
public String returnFAQPage()
{
return("Your FAQ JSP Page Location Here";
}
等等...
您可以修改它以仅返回您的内容页面变量(如果您愿意)。
当然也可以。
让我知道你是怎么办的。