spring MVC两个表单,带有一个控制器

时间:2015-05-28 12:35:44

标签: spring spring-mvc

这是Jsp文件。我想要一个只在一个页面中处理两个表单的控制器。有没有办法处理这种形式?我创建一个控制器,但它不能绑定表单。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"   %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Welcome to Spring Web MVC project</title>
</head>

<body>
   <form:form method="POST" modelAttribute="logindetails" name ="LoginForm" autocomplete="off" >

            Dept_Code :

          <form:input path="departmentcode"/>


          Dept_name
          <form:input path="departmentname" placeholder="name" />



  <button type="submit" >Save</button>



   </form:form><br/>

   <form:form method="POST" modelAttribute="emp_details" name = "emp_Form" autocomplete="off" >

            Emp_Code :

          <form:input path="emp_code"/>


          Emp_Coded
          <form:input path="Emp_coded" placeholder="coded" />

          DDO_Desc
          <form:input path="ddo_desc" placeholder="DDO_DESC" />



  <button type="submit" >Save</button>



   </form:form>

</body>
</html>

如果你需要的话,这是我的控制器类。但我只绑定一种形式。

@Controller
public class DepartmentController {
@Autowired
Deptservices deptservices;

 @RequestMapping(value="index", method= RequestMethod.GET)
public ModelAndView insert(@ModelAttribute("logindetails") Department insert)
{
    ModelAndView mav=new ModelAndView("index");
    return mav;
}

 @RequestMapping(value="index", method= RequestMethod.POST)
public ModelAndView insertPost(@ModelAttribute("logindetails") Department insert)
{
    ModelAndView mav=new ModelAndView("index");
    deptservices.insert(insert);
    return mav;
}
}

2 个答案:

答案 0 :(得分:0)

您已使用名称logindetails绑定模型。

因此,请以第二种形式将modelAttribute="emp_details"更改为modelAttribute="logindetails"

答案 1 :(得分:0)

采用像

这样的视图模型
EmployeeViewMOdel{
logindetails
emp_details
}

model.addAttribute("EmployeeViewMOdelObj",EmployeeViewMOdel);
<form:form method="POST" modelAttribute="*EmployeeViewMOdelObj*" name ="LoginForm" autocomplete="off" >


<form:form method="POST" modelAttribute="EmployeeViewMOdelObj" name = "emp_Form" autocomplete="off" >

将此对象绑定到两个表单,然后单个控制器可以处理您的请求。