使用Spring,Hibernate,JSP创建下拉列表

时间:2014-10-22 00:21:10

标签: java spring hibernate jsp spring-mvc

应用程序:Hibernate,Spring 3.0 MVC,JSP(使用Spring表单)

要求:使用Hibernate从数据库中选择表数据,并使用Spring MVC将其显示在JSP页面中的下拉列表中。

代码: Hibernate / 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 个答案:

答案 0 :(得分:1)

我认为jsp应该是

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