【问题标题】:Can not populate drop down list using hibernate query in spring mvc无法在spring mvc中使用hibernate查询填充下拉列表
【发布时间】:2011-09-30 19:27:08
【问题描述】:

这是我的控制器。在此我使用参考数据将列表发送到我的 JSP 页面。将列表读入 countryList,其中包含使用休眠查询填充的所有国家/地区名称的列表

@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);
    }
}

这是我的 DAO 实现:

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");
    }
}

这是我的 JSP 代码:

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

但下拉菜单中没有填充任何内容。

【问题讨论】:

  • 您的 DAO 是否经过测试?工作正常吗?
  • 请考虑使用标准 Java 命名约定。

标签: spring model-view-controller


【解决方案1】:

将国家列表命名为代表多个国家的名称,例如“国家”。

还有一个客户国家属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多