2014-09-27 83 views
0

我是新來的spring mvc,我按照http://docs.spring.io/docs/Spring-MVC-step-by-step/中詳細描述的例子,但是當我嘗試運行它時,我得到了這個評論末尾描述的異常。 我一直在做一些研究,但我找不到這個問題的答案。有誰知道我做錯了什麼?如何在spring中定義bean mvc

<?xml version="1.0" encoding="UTF-8"?> 

<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="springDemo.controller" /> 

    <beans:bean id="productManager" class="springDemo.service.SimpleProductManager"> 
     <beans:property name="products"> 
      <beans:list> 
       <beans:idref bean="product1"/> 
       <beans:idref bean="product2"/> 
       <beans:idref bean="product3"/> 
      </beans:list> 
     </beans:property> 
    </beans:bean> 

    <beans:bean id="product1" class="springDemo.domain.Product"> 
     <beans:property name="description" value="Lamp"/> 
     <beans:property name="price" value="5.75"/> 
    </beans:bean> 

    <beans:bean id="product2" class="springDemo.domain.Product"> 
     <beans:property name="description" value="Table"/> 
     <beans:property name="price" value="75.25"/> 
    </beans:bean> 

    <beans:bean id="product3" class="springDemo.domain.Product"> 
     <beans:property name="description" value="Chair"/> 
     <beans:property name="price" value="22.79"/> 
    </beans:bean> 

    <beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <beans:property name="basename" value="messages"/> 
    </beans:bean> 

</beans:beans> 

當我嘗試運行此我得到這個例外

java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [springDemo.domain.Product] for property 'products[0]': no matching editors or conversion strategy found 
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241) 
    at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:541) 
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:179) 
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) 
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516) 
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) 
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 

UPDATE:我忘了提,我已經安裝了春天工具套件3.6.1和我創建了一個春天web項目。

回答

0

我認爲你必須從窗體輸入產品,所以Spring無法將輸入字符串轉換爲產品類型。爲此,你必須編寫一個屬性編輯器。

屬性編輯器允許您在一個實例Product和它們的String表示的自定義對象之間進行轉換。

該異常告訴Spring不能自動從POST字符串值轉換爲實際的「產品」對象。

檢查spring property editor

+0

我沒有一個形式,我所有的代碼是在一步一步的解釋。基本上我有複製粘貼代碼,但它不起作用。唯一的區別是我替換 Martin 2014-09-27 18:17:40

+0

我沒有粘貼代碼的其餘部分,因爲我不想讓這個帖子更大,但如果你想我可以添加任何你需要的代碼。 – Martin 2014-09-27 18:38:26

相關問題