Results 1 to 6 of 6
  1. #1
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default could not able to set property from springapp-servlet.xml, throws Failed to convert

    i am trying to set the value from the springapp-servlet.xml and it fails to set the property by giving error message

    Failed to convert property value of type [java.lang.String] to required type [ProductManager] for property 'productManager'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [ProductManager] for property 'productManager': no matching editors or conversion strategy found

    my code
    ---------

    springapp-servlet.xml
    ---------------------------------

    Java Code:
    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
    -----------------


    Java Code:
    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
    ------------------------------

    Java Code:
    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;  
        }  
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: could not able to set property from springapp-servlet.xml, throws Failed to conve

    It's 'ref="productManager" not 'value="productManager".
    'ref' is used for beans.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: could not able to set property from springapp-servlet.xml, throws Failed to conve

    yes it has worked, thanks very much..

  4. #4
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: could not able to set property from springapp-servlet.xml, throws Failed to conve

    another thing, i the jsp i had included the jstl, and messages.properties contains the below one.i call from the jsp using the messages.properties.

    but the when i try to run, the tag is not showing.. it is diplaying like this.

    ???heading???

    ???greeting??? Tue Nov 27 10:33:12 SGT 2012


    messages.properties (\HelloSpring\build\web\WEB-INF\classes)
    ----------------------------------------------------------------


    Java Code:
    title=SpringApp
    
    heading=Hello :: SpringApp
    
    greeting=Greetings, it is now
    JSP code (hello.jsp)
    ---------


    Java Code:
    <%@ include file="/WEB-INF/jsp/include.jsp" %> 
    
    <html>
    
        <head><title><fmt:message key="title"/> </title></head>
    
      <body>
    
        <h1>Hello - Spring Application</h1>
    
        <!-- <p>Greetings.it is now ${now}  </p> -->
        <!-- <p>Greetings.it is now <c:out value ="${now}" />  </p>  -->
        
        <h1><fmt:message key="heading"/></h1>
    
        <p><fmt:message key="greeting"/> <c:out value="${model.now}"/></p>
    
        <h3>Products</h3>
    
        <c:forEach items="${model.products}" var="prod">
    
          <c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>
    
        </c:forEach>
    
      
      </body>
    
    </html>
    [/CODE]
    Last edited by cgk_js; 11-27-2012 at 04:05 AM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: could not able to set property from springapp-servlet.xml, throws Failed to conve

    First, have you defined your javax.servlet.jsp.jstl.fmt.localizationContext in the web.xml?
    That tells the i18n tags what bundle to use.
    Second, the bundle name should end '_<2char code>', so english would be 'messages_en.properties'.
    You probably also want to define a fallback, javax.servlet.jsp.jstl.fmt.fallbackLocale.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: could not able to set property from springapp-servlet.xml, throws Failed to conve

    i got the problem being resolved..

    following are the step's i did it..

    1) in the springapp-context.xml (dispacher ), i add the below

    XML Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages"/> 
    </bean>
    2) second, i placed the messages.properties file inside the /WEB-INF/classes. make sure that after clean and deploy the file stills exits.

Similar Threads

  1. throws
    By java4amanda in forum New To Java
    Replies: 2
    Last Post: 03-29-2012, 09:08 AM
  2. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  3. Replies: 0
    Last Post: 06-25-2009, 05:28 AM
  4. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 01:07 AM
  5. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •