我们如何在spring mvc中将对象从视图传递给控制器​​?

时间:2016-04-01 21:30:23

标签: spring spring-mvc model-view-controller thymeleaf

控制器1:

@RequestMapping(value = "/allEmployees", method = RequestMethod.GET)
public String getAllEmployees(@ModelAttribute("employeeListForm") EmployeeListForm employeeListForm, HttpServletRequest request, HttpServletResponse response, Model model, BindingResult errors) throws Exception {
    List<Employee> subList = null;
    allEmployees = empService.getAllEmployees(categoryId);      
    employeeListForm.setAllEmployees("allEmployees");           
    employeeListForm.setCategory("IT");         
    model.addAttribute("etcSearchForm", etcSearchForm); 
    return ALL_EMP_VIEW;
}

控制器2:

@RequestMapping(value = "/EmployeeDetail", method = RequestMethod.GET)
public String getEmpDetail((@ModelAttribute("empDetailForm") EmployeeDetailForm empDetailForm, HttpServletRequest request, HttpServletResponse response, Model model, BindingResult errors) throws Exception {

/*there is no direct approach to get the employee by id how                    should I get the 
  employee object from list page to deatil page 
*/
    empDetailForm.setSalary(emp.getSalary());
    empDetailForm.setTitle(emp.getTitle());
    empDetailForm.setName(emp.getName());
    model.addAttribute("empDetailForm", empDetailForm); 
    return ALL_EMP_Detail_Form;
}

查看:

<div th:if="${allEmplListForm.aAllEmployees.size() > 0}" th:each="emp : ${allEmplListForm.aAllEmployees}" th:object="${emp}" >                           
    <a th:href="@{/EmployeeDetail)}" class="link">
        <div><p class="name" th:text="*{name}"></p></div>
        <div><p th:text="*{date}"></p> <p th:text="*{title}"></p></div>
        <div><p class="salary" th:text="*{salary}"></p></div>
    </a>
</div>

我在一个页面中显示员工列表,当我点击员工时,它必须带我到详细信息页面(控制器2),其中没有直接的服务方法来通过id或名称或其他方式获得员工。

我应该如何将我的员工对象从视图发送到控制器。点击链接,它应该进入详细页面。

1 个答案:

答案 0 :(得分:0)

您的问题就像thisthis one。我认为除了make <form>元素之外别无选择,并将Employee对象发布到控制器。