2

Applications: Hibernate, Spring 3.0 MVC, JSP (used Spring forms)

Requirement: To select a table data from the database using Hibernate and display that as a drop-down list in JSP page using Spring MVC.

Code: Hibernate / Dao code is

Cuisine class

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

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

.. getters and setters

RecipeDaoImpl class

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 page:

<%@ 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>

On doing it, I am getting following error:

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)

Can someone please suggest how to fix this? I have checked couple of articles and tried to replicate them, but no luck.

1 Answer 1

1

I think jsp should be

<td><sf:select path="${cuisine}">
         <sf:options items="${cuisineList}" id="id" itemValue="name"></sf:options>
    </sf:select></td>
</tr>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.. but it is not giving value as "[id=1, name=Continental]" in the drop down box.. any way I can just get name in the JSP page?
Many thanks for the pointer.. I have added "<sf:options items="${cuisineList}" itemValue="id" itemLabel="name" />" and it worked..
I'm getting this error while doing so... java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name, any clues?
one more error ERROR: org.springframework.web.servlet.tags.form.SelectTag - Neither BindingResult nor plain target object for bean name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.