有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Spring、Hibernate和JSP创建下拉列表

应用程序: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();
}

春季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)

有人能建议如何解决这个问题吗?我查阅了几篇文章,并试图复制它们,但运气不佳


共 (0) 个答案