开发者

Can not populate drop down list using hibernate query in spring mvc

Here is my controller. In this I am using the reference data to send the List to my JSP page. Read the list into countryList which contains list of all country names populated using hibernate query

@SuppressWarnings("deprecation")
public class customerController extends SimpleFormController {

    public customerController() {
        setCommandClass(customer.class);
        setCommandName("customer");
    }

    private customerDAO customerDAO;

    public void setcustomerDAO(customerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

  开发者_开发知识库  @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        customer customer = (customer) command;
        customerDAO.savecustomer(customer);
        return new ModelAndView("customerSuccess");
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();
        List countryList = customerDAO.listCountries();
        referenceData.put("country", countryList);
        return referenceData;
    }

    public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelMap modelMap = new ModelMap();
        return new ModelAndView("customer", modelMap);
    }
}

Here is my DAO implementation:

public class customerDAOImpl implements customerDAO {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public void savecustomer(customer customer) {
        hibernateTemplate.saveOrUpdate(customer);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<customer> listcustomer() {
        return hibernateTemplate.find("from customer");
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<countries> listCountries() {
        return hibernateTemplate.find("from customer.countries");
    }
}

Here is my JSP code:

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="form"   uri="http://www.springframework.org/tags/form" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <form:form commandName="customer" name="customer" method="post">
    <tr>
          <td align="right" class="para">Country:</td>
          <td align="left"><span id="spryselect2">
          <form:select path="country" id="country">
               <form:options items="${country}" itemValue="country_id" itemLabel="name" />
            </form:select>
            <span class="selectRequiredMsg">Please select country.</span></span></td>
        </tr>

But nothing is getting populated into the drop down.


Name the list of countries something representing multiple countries, like, say, "countries".

There's also a customer country property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜