Populating a DropDown Menu From a SQL Database Usi

2020-07-25 03:15发布

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.

2条回答
▲ chillily
2楼-- · 2020-07-25 03:28
<form:options value="" items="${job.jobName}"/> 

An other fail is the dropdown value will be always nothing because of value="".

查看更多
够拽才男人
3楼-- · 2020-07-25 03:29

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>
查看更多
登录 后发表回答