【问题标题】:could not able to set the property from the springapp-servlet.xml throws Failed to convert property无法从 springapp-servlet.xml 设置属性抛出 Failed to convert property
【发布时间】:2012-11-26 18:46:25
【问题描述】:

我正在尝试从 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;  
    }  
}  

【问题讨论】:

    标签: spring


    【解决方案1】:

    改变

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

    到:

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

    现在您正在将HelloControllerproductManager 属性设置为"productManager" String,并且您(可能)想要将其与上下文中前面定义的productManager bean 连接起来。

    【讨论】:

    • 是的,谢谢,我可以参考 productManager。
    猜你喜欢
    • 2022-08-02
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多