中获取模型属性的值时
嗨,在我的spring hibernate注释应用程序中,我使用jsp作为视图。在jsp我加载值从我的mysql数据库下拉列表。但是我不能将所选值从jsp页面传输到控制器。我有两个表雇员表和团队表在mysql。我想显示所选团队中的所有员工。例如,如果我选择team1从下拉菜单,我想在team1中显示所有员工。在点击提交按钮时,我得到了空点异常错误。
加载mysql数据库值到下拉列表的JSP页面代码片段
<form:form method="POST" action="Search.html">
<table>
<tr>
<td>
<form:label path="teams.teamId" id="teams">Team Name</form:label>
</td>
<td>
<form:select path="teams.teamId" cssStyle="width: 150px;">
<option value="-1">Select a type</option>
<c:forEach items="${teamKey}" var="teams">
<option value="${teams.teamId}" >${teams.teamName}</option>
</c:forEach>
<tr>
<td colspan="2"><input type="submit" value="submit"> </td>
</tr>
</form:select>
</tr>
</table>
</form:form>
Hibernate select query in resourcedaoimpl
@Override
public void serchResources(int teamid) {
// TODO Auto-generated method stub
System.out.println("teamid is given---------- :"+teamid);
sessionfactory.getCurrentSession().createQuery("FROM Resource WHERE teamId=" +teamid);
System.out.println("After query teamid is given--************* :"+teamid);
}
contrtoller类
@RequestMapping(value="/Search",method = RequestMethod.POST)
public ModelAndView fromSearch(@ModelAttribute("command") Resource resource,BindingResult result){
return new ModelAndView("redirect:/searchResult.html");
}
@RequestMapping(value="/searchResult",method = RequestMethod.GET)
public ModelAndView searchResult(@ModelAttribute("command") Resource resource,BindingResult result){
Map<String, Object> model = new HashMap<String, Object>();
//to print employee table
model.put("resourcekey",resourceServices.listResources());
//lists teams
model.put("teamKey", addteamServices.listTeams());
return new ModelAndView("Search",model);
}
谢谢你的建议
您需要在表单标签中包含modelAttribute
,
<form:form method="POST" action="Search.html" modelAttribute="command" >
当你试图在控制器