问题
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 :(
回答1:
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.
回答2:
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}">
回答3:
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()
来源:https://stackoverflow.com/questions/7318004/javax-el-propertynotfoundexception-the-class-java-lang-string-does-not-have-t