2014-10-22 55 views
2
下拉列表

應用:休眠,春季3.0 MVC,JSP(使用彈簧形式)創建使用Spring,Hibernate的,JSP

要求:要使用Hibernate從數據庫中選擇一個表中的數據並顯示,作爲一個掇使用Spring MVC在JSP頁面下拉列表。

代碼: 休眠/ DAO代碼是

美食類

@Entity 
@Table(name = "cuisine") 
public class Cuisine { 
    @Id 
    @Column(name = "id") 
    private int id; 

    @Column(name = "name") 
    private String name; 

.. getters and setters 

RecipeDaoImpl類

public List<Cuisine> getCuisine() { 
    String hql = "SELECT id, name FROM Cuisine"; 
    return getSession().createQuery(hql).list(); 
} 

Spring MVC的

@Controller 
public class RecipeController { 
... 
    @RequestMapping("/add") 
    public String newRecipe(Map<String, Object> map) { 
     /* Get cuisine list and new object for cuisine */ 
     List<Cuisine> cuisines = recipeServices.getCuisine(); 
     System.out.println(cuisines); 
     map.put("cuisineList", cuisines); 
     map.put("cuisine", new Cuisine()); 

     return "recipes/new"; 
    } 

JSP頁面:

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%> 

<tr> 
    <th><sf:label path="cuisine">Cuisine</sf:label></th> 
</tr> 

<tr> 
    <td><sf:select path="${cuisineList}"> 
      <sf:options items="${cuisine}"></sf:options> 
     </sf:select></td> 
    </tr> 

做下去,我收到以下錯誤:

org.springframework.beans.NotReadablePropertyException: Invalid property '[Cuisine [id=1, name=Continental][id=2, name=Italian]' of bean class [com.recipe.tables.Recipe]: Bean property '[Cuisine [id=1, name=Continental][id=2, name=Italian]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 
    org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:729) 
    org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:721) 
    org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99) 
    org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:219) 
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120) 

可有人請建議如何解決這一問題?我檢查了幾篇文章,試圖複製它們,但沒有運氣。

回答

1

我認爲JSP應該是

<td><sf:select path="${cuisine}"> 
     <sf:options items="${cuisineList}" id="id" itemValue="name"></sf:options> 
    </sf:select></td> 
</tr> 
+0

謝謝..但它不給價值「[ID = 1,名稱=大陸]」,在下拉框中..什麼辦法可以先手名在JSP頁面中? – Sandeep 2014-10-22 02:39:29

+0

非常感謝指針..我已經添加「」,它的工作.. – Sandeep 2014-10-22 04:30:48