我需要使用单个控制器类为具有不同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
感谢您的帮助
答案 0 :(得分:0)
你能不能只在你的一个控制器中为你的每个页面添加使用@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";
}
等...
如果您愿意,可以将其修改为仅返回您的内容页面变量。
这肯定也会奏效。
让我知道你是怎么过的。
答案 1 :(得分:0)
为什么不利用新的春季@Controller
和@RequestMapping
注释?这样你只需要一个控制器。
@Controller
public class StaticController {
@RequestMapping({"/index.dlg", "/howto", // The rest of your mappings}
public String handleStatic(HttpServletRequest request, Model model) {
String contentPage = //Extract page-name from request
model.addObject("mainContent", "maincontent/" + contentPage);
model.addObject("mainMenuSelect", contentPage);
return "common/mainlayout";
}
}
然后可以使用RequestMappingHandlermapping
检测并设置此控制器,并由RequestMappingHandlerAdapter
执行(在xml配置中使用<mvc:annotation-driven />
注册)。
或者使用spring view-controller(<mvc:view-controller .. />
)并添加一个拦截器来为模型添加所需的属性
<mvc:view-controller path="/index.dlg" view-name="common/mainlayout" />
<mvc:view-controller path="/faq.dlg" view-name="common/mainlayout" />
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/index.dlg" />
<mapping path="/faq/dlg" />
<bean class="YourInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
这种方法的缺点是你基本上复制了你的映射(一次用于控制器,一次用于拦截器)。
链接