【问题标题】:pass value of selcted item in a dropdownlist to controller in spring mvc hibernate annottations将下拉列表中选定项目的值传递给spring mvc休眠注释中的控制器
【发布时间】:2014-09-27 19:01:58
【问题描述】:

嗨,在我的 spring hibernate 注释应用程序中,我使用 jsp 作为视图。在 jsp 中,我将值从我的 mysql 数据库加载到下拉列表中。但我无法将选定的值从 jsp 页面传输到控制器。我在 mysql 中有两个表 employee 表和 team 表。我想显示所选团队中的所有员工。例如,如果我从下拉列表中选择 team1,我想显示 team1 中的所有员工。单击提交按钮时出现空指针执行错误。

我的jsp页面代码sn-p用于加载mysql数据库值到下拉列表

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

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

}

控制器类

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

感谢您的建议..

【问题讨论】:

    标签: mysql jsp jakarta-ee spring-mvc hibernate-annotations


    【解决方案1】:

    您需要在表单标签中包含modelAttribute

    <form:form method="POST" action="Search.html" modelAttribute="command" >
    

    因此,当您尝试在控制器中获取它们时,您将获得模型属性的值

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多