使用Spring,Hibernate填充下拉列表

时间:2015-04-28 10:37:42

标签: spring hibernate spring-mvc

专家,

Class EmployeeEntity
    {
        id
        Fname
        Salary
        Phone
    }

Controller.java

@RequestMapping(value = "/list")
public String listEmployee(ModelAndView map)
{

    map.addObject("employee", employeeManager.getEmp());

    return "emp";


}

EmployeeDAO.java

@Override
public List<EmployeeEntity> getEmp() {
    return this.sessionFactory.getCurrentSession().createQuery("select e.id, e.firstname from EmployeeEntity e").list();
}

emp.jsp

<form:form method="POST">

 <form:select path="Fname">
    <form:options items="${employee}" />
</form:select>

</form:form>

这是我的档案。我没有在drropdown中找到价值。请帮助我如何找出问题。我需要使用Fname填充列表,并将Id作为列表中的值

错误

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)

1 个答案:

答案 0 :(得分:1)

首先你必须使用map而不是list。您可以参考以下代码:

@RequestMapping(value = "/list")
public Map listEmployee(ModelAndView map) throws Exception {
    Map referenceData = new HashMap();
    Map<String,String> country = new LinkedHashMap<String,String>();
    country.put("US", "United Stated");
    country.put("CHINA", "China");
    country.put("SG", "Singapore");
    country.put("MY", "Malaysia");
    referenceData.put("countryList", country);
}

在JSP上迭代Map以构建下拉列表,如下所示:

<form:select path="country">
   <form:option value="NONE" label="--- Select ---"/>
   <form:options items="${countryList}" />
</form:select>