简体   繁体   中英

Populating a DropDown Menu From a SQL Database Using Hibernate in Spring MVC

I've done similar things to this before, but never using this particular configuration. Every example I look up shows the dropdown having the options set within the controller, when I don't want them set line by line in the controller, but pulled from a column in a SQL database.

I have other forms that currently pull from the same table and column, but with the drop down. I get nothing. Here is the JSP

This is what I have in the jsp, previously I used a c:forEach , and I suspect I might have to put that back in used in conjunction with with jsp use bean...

<table>
    <tr>
        <td>Job:</td>
        <td>    
            <form:select path="Job.jobName">
                <form:option value="" label="Select Job"/>
                <form:options value="" items="${job.jobName}"/>
            </form:select>
        </td>
        <td><form:errors path="job.jobName" /> </td>
    </tr>
</table>

This is the method call in the controller, there is more than this, but it's what I"m using..

List<Job> jobList = jobService.listjobsByPage(page);

and here is the query into the DAOImpl

public List<Job> getDataByJobName(String jobName) {            
     Session session = sessionFactory.openSession();
     List<Job> result = null;
     try{
            session.beginTransaction();
            Query query = session.createQuery("from Job where upper(jobName) like ? " +
                         "order by jobName");
            query.setParameter(0, "%" + jobName + "%");
            result = query.list();
            session.getTransaction().commit();
            session.close();
     } catch(Exception e){
            e.printStackTrace();
     }
     return result;
}

If anyone can even point me in the right direction on how to set this up that would be a great help.

Thanks in advance.

You have to do first :

    ModelAndView model = new ModelAndView("index");
    model.addObject("lists", list);

than

    <form:select path="list">
        <form:options items="${lists}" />
    </form:select>
<form:options value="" items="${job.jobName}"/> 

另一个失败的原因是,由于value =“”,下拉值始终为空。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM