0

springapp-servlet.xmlから値を設定しようとしていますが、エラーメッセージが表示されてプロパティを設定できません。

タイプ[java.lang.String]のプロパティ値をプロパティ'productManager'に必要なタイプ[ProductManager]に変換できませんでした。ネストされた例外はjava.lang.IllegalArgumentExceptionです:タイプ[java.lang.String]の値をプロパティ'productManager'に必要なタイプ[ProductManager]に変換できません:一致するエディターまたは変換戦略が見つかりません

私のコード

springapp-servlet.xml

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:p="http://www.springframework.org/schema/p"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:tx="http://www.springframework.org/schema/tx"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  


       <bean id="productManager" class="SimpleProductManager">  
        <property name="products">  
            <list>  
                <ref bean="product1"/>  
                <ref bean="product2"/>  
           </list>  
        </property>  

    </bean>   

    <bean id="product1" class="Product">  
        <property name="description" value="Chair"/>  
    </bean>   

    <bean id="product2" class="Product">  
        <property name="description" value="Desk"/>  
    </bean>   

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

    <bean name="/hello.htm" class="HelloController">   
            <property name="productManager" value="productManager"/>      
    </bean>         

    <!-- we can prefix="/"   
    http://localhost:8080/HelloSpring/hello.htm  
    specify the path in  modelview from the controller   
                        OR  
    Decouple the view from the controller                      
    prefix="/WEB-INF/jsp/"  
    -->  
            <bean id="viewResolver"  
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
              p:prefix="/WEB-INF/jsp/"  
              p:suffix=".jsp" />  
</beans>  

HelloController

view plaincopy to clipboardprint?
/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */  

/** 
 * 
 * @author gopc 
 */  
import java.util.Date;  
import java.util.Map;  
import java.util.HashMap;  

import org.springframework.web.servlet.mvc.Controller;  

import org.springframework.web.servlet.ModelAndView;   

import javax.servlet.ServletException;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse;   

import org.apache.commons.logging.Log;  

import org.apache.commons.logging.LogFactory;   

import java.io.IOException;  



public class HelloController implements Controller {   

      private ProductManager productManager;   
    protected final Log logger = LogFactory.getLog(getClass());   


    //Writing some business logic in the controller  

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException  
    {  
        String now = (new java.util.Date()).toString();  

        logger.info("returning hello view with " + now);   
        Map<String, Object> myModel = new HashMap<String, Object>();  
        myModel.put("now", now);  
        myModel.put("products", this.productManager.getProducts());   
        return new ModelAndView("hello", "model", myModel);  
    }  

    public void setProductManager(ProductManager productManager)   
    {  
        this.productManager = productManager;  
    }   

}  

SimpleProductManager

view plaincopy to clipboardprint?
/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */  

/** 
 * 
 * @author gopc 
 */  

import java.util.List;  
import org.apache.commons.logging.LogFactory;  
import org.apache.commons.logging.Log;  


public class SimpleProductManager implements ProductManager{  

    private List<Product> products;      
    protected final Log logger = LogFactory.getLog(getClass());   

    public List<Product> getProducts()  
    {  
       logger.info("inside  getProducts ");  
        return products;  
    }  

    public void increasePrice (int per)  
    {  
        if (products != null)  
        {  
            for (Product prod : products)  
            {  
                double newPrice = prod.getPrice() * (100 + per) /100;  
                prod.setPrice(newPrice);  
            }  
        }  
    }  

    public void setProducts(List <Product> products )  
    {  
        logger.info("inside  setProducts ");  
        this.products = products;  
    }  
}  
4

1 に答える 1

0

変化する

<property name="productManager" value="productManager"/> 
<!-- sets "productManager" to "productManager" String -->

に :

<property name="productManager" ref="productManager"/>
<!-- sets "productManager" to bean with id="productManager" -->

現在、 のproductManagerプロパティをHelloControllerに設定していますが"productManager" String、(おそらく) 必要なのはproductManager、コンテキストで以前に定義された Bean に接続することです。

于 2012-11-26T10:46:25.010 に答える