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>  

你好控制器

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;  
    }   

}  

简单产品经理

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为 a "productManager" String,而您(可能)想要的是将它与productManager上下文中之前定义的 bean 连接起来。

于 2012-11-26T10:46:25.010 回答