Chandana
Chandana

Reputation: 2638

javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property

I am creating Sample Spring MVC application. In my Controller class I have define like this:

Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("now", now);
        myModel.put("products", this.productManager.getProducts());

        return new ModelAndView("hello", "model", myModel);

When I put following part in my JSP file i got javax.el.PropertyNotFoundException exception

<c:forEach items="${model.products}" var="prod">
        <c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>
    </c:forEach>

Here is my full exception :

javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'description'.

But in my domain class private Sting description property has public getter and setter. That Product class is public one.

Product class:

public class Product implements Serializable {
    private String description;
    private Double price;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

}

PS:

If I used like this it's working

<c:forEach items="${model.products}" var="prod"  varStatus="status">        
        <c:out value="${model.products[status.count -1].description}"/> <i>$<c:out value="${model.products[status.count -1].price}"/></i><br><br>
    </c:forEach> 

But recommended solution not working :(

Upvotes: 5

Views: 25978

Answers (3)

loreii
loreii

Reputation: 398

This happen when you are passing a String to an EL operation c:forEach instead of an iterable element, in the following example I miss the '$' so it's passing the exact String without variable resolution.

<c:forEach var="o" items="{operations}">

The next is right because operation is a String[] in my code

<c:forEach var="o" items="${operations}">

Upvotes: 5

Paul Grime
Paul Grime

Reputation: 15104

Maybe check your taglib import:

OLD

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

NEW

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

Is your Product class and its getters accessible? By this I broadly mean are they public?

See http://forum.springsource.org/showthread.php?58420-Problem-with-javax.el.PropertyNotFoundException.

Upvotes: 3

NimChimpsky
NimChimpsky

Reputation: 47300

Try this:

<c:forEach items="${model['products']}" var="oneProduct">
    <c:out value="${oneProduct.description}"/> <i>$<c:out value="${oneProduct.price}"/>                            
    </i><br><br>
</c:forEach>

And check the capitalization of you gettters and setters, should be getDescription()

Upvotes: 0

Related Questions