在Spring MVC中使用Hibernate从SQL数据库填充DropDown菜单 [英] Populating a DropDown Menu From a SQL Database Using Hibernate in Spring MVC
问题描述
我之前做过类似的事情,但从未使用过这种特殊的配置。我查找的每个示例都显示了在控制器中设置选项的下拉列表,当我不希望它们在控制器中逐行设置时,而是从SQL数据库中的列中拉出。
我有其他表格,当前从同一个表格和列中拉出来,但是下拉下来。我什么也没得到。这里是JSP
这就是我在jsp中所使用的,以前我用过一个 c:forEach
,并且我怀疑我可能不得不把它与jsp use bean一起使用......
< table>
< tr>
< td>工作:< / td>
< td>
< form:select path =Job.jobName>
< form:option value =label =选择工作/>
< form:options value =items =$ {job.jobName}/>
< / form:select>
< / td>
< td>< form:errors path =job.jobName/> < / TD>
< / tr>
< / table>
这是控制器中的方法调用,除此之外,这是我的使用..
列表< Job> jobList = jobService.listjobsByPage(page);
这里是对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 (例外e){
e.printStackTrace();
}
返回结果;
}
$ c $如果任何人甚至可以指出我在正确的方向如何设置这将是一个很大的帮助。
在此先感谢您。
解决方案 您必须先做:
ModelAndView model = new ModelAndView(index);
model.addObject(lists,list);
比
< form:select path =list>
< form:options items =$ {lists}/>
< / form:select>
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>
这篇关于在Spring MVC中使用Hibernate从SQL数据库填充DropDown菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!