2

大家好,是否可以在单个 jsp 上使用多个表单,也可以使用单个按钮?

这是我的jsp页面,我有两个表单,我知道是这样,它只保存第二个表单。

<html>
<head>
<title>Update General Info</title>
<script type="text/javascript">
function validateForm()
{
var name=document.getElementById("name").value;
 var surname=document.getElementById("surname").value;
var email=document.getElementById("email").value;
var amka=document.getElementById("amka").value;
if (name.length == 0)   
  {
  alert("Name must be filled out");
  return false;
 } else if(surname.length == 0){
      alert("Surname must be filled out");
      return false;
  }else if(email.length == 0){
      alert("Email must be filled out");
      return false;
  }else if(amka.length == 0){
      alert("Amka must be filled out");
      return false;
  }
}
</script>
</head>
<body>
<h1>Update General Info</h1>

<c:if test="${!empty user}">
<c:url var="saveArticleUrl" value="/articles/updateGeneralSave.html" />
<form:form onsubmit="return validateForm()" modelAttribute="user" method="POST" >
<table bgcolor="DBEADC" border=1>
<tr>
<th>Id</th>
<th>Team</th>
<th>Name</th>
<th>Surname</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>AMKA</th>
<th>Status</th>
<th>Department</th>
</tr>


<tr>
<td><form:input readonly="true" path="id" value="${user.id}"></form:input></td>
<td><form:input readonly="true" path="team" value="${user.team}"></form:input></td>
<td><form:input id="name" path="name" value="${user.name}"></form:input></td>
<td><form:input id="surname" path="surname" value="${user.surname}"></form:input></td>
<td><form:input readonly="true" path="username" value="${user.username}"></form:input></td>
<td><form:input type="password" readonly="true" path="password" value="${user.password}"></form:input></td>
<td><form:input id="email" path="email" value="${user.email}"></form:input></td>
<td><form:input id="amka" path="amka" value="${user.amka}"></form:input></td>
<td><form:input id="status" path="status" value="${user.status}"></form:input></td>

<td><form:select  path="department">
<c:forEach items="${departments}" var="dep">
<c:if test="${dep.dep_name==user.department }">
<OPTION selected VALUE="${dep.dep_name}"><c:out value="${dep.dep_name}"/></OPTION>
</c:if>
<c:if test="${dep.dep_name!=user.department }">
<OPTION VALUE="${dep.dep_name}"><c:out value="${dep.dep_name}"/></OPTION>
</c:if>

</c:forEach>
</form:select></td>
</tr>
</table>




</form:form>
</c:if>
<c:if test="${!empty phones}">
<c:url var="saveArticleUrl" value="/articles/updatePhoneSave.html" />
<form:form onsubmit="return validateForm()" modelAttribute="updatePh" method="POST" action="${saveArticleUrl}">
<table bgcolor="DBEADC" border=1>
<tr>
<th>Id</th>
<th>Phone</th>
<th>Mobile</th>
<th>Fax</th>
</tr>



<tr>
<td><form:input readonly="true" path="id" value="${phones.id}"></form:input></td>
<td><form:input id="phone" path="phone" value="${phones.phone}"></form:input></td>
<td><form:input id="mobile" path="mobile" value="${phones.mobile}"></form:input></td>
<td><form:input path="fax" value="${phones.fax}"></form:input></td>
</tr>


</table>



<input type="submit" value="Update" />
</form:form>

</c:if>
</body>
</html>

和控制器

RequestMapping(value = "updateGeneral" , method = RequestMethod.GET)
        public ModelAndView updateGeneral(@ModelAttribute("user") Users user ,@ModelAttribute("updatePh") Phone updatePh, @RequestParam("id")Integer id){
         Map<String, Object> model = new HashMap<String, Object>();
            model.put("user",  articleService.getUser(id));
            model.put("departments", articleService.listDepartments());
            //twra mpike
            model.put("phones",  articleService.getPhones(id));
        return new ModelAndView("updategeneral",model);
     }
     //evala akoma ena modelattri
     @RequestMapping(value = "updateGeneralSave" , method = RequestMethod.POST)
        public ModelAndView updateGeneralSave(@ModelAttribute("user") Users user){


         articleService.updateUser(user);

        return new ModelAndView("redirect:/articles/listusers.html");
     }


 @RequestMapping(value = "updatePhoneSave" , method = RequestMethod.POST)
        public ModelAndView updatePhonesave(@ModelAttribute("updatePh") Phone updatePh){


         articleService.updatePhone(updatePh);  
        return new ModelAndView("redirect:/articles/listusers.html");
     }
4

5 回答 5

4

一个 JSP 中可以有多个表单,但不能同时发送两个表单。

您应该混合两种形式和两种操作,检索操作/控制器中的所有信息并保存电话和用户信息。另一种选择是使用 Ajax 发送一个表单并像往常一样发送另一个。

顺便说一句,您的问题与Spring无关。

于 2012-06-22T11:51:05.803 回答
0

您唯一的选择是为 ajax 做,您必须意识到,如果您的 Controller 方法将在第一次提交后呈现页面,那么您通过 HTTP 进行的第二次提交将永远不会生效。

    function submitForm(form, index){
       $.ajax({
        dataType: "json",
        method: "POST",
        url: "your controller url",
        data:$('#form').serialize(),
        success: function (data) {
           if(index > 0){
              submitForm(form+=1, index--)
           }
        }
    });

}

于 2014-05-30T09:21:30.137 回答
0

是的,当然,你可以让你的按钮来提交你的表格。但是你已经用 ajax 做到了。

于 2012-06-21T17:08:37.587 回答
0

您需要使用 document.forms[i] 遍历网页上的表单,并使用每个表单单独调用提交

于 2013-05-02T13:16:07.640 回答
-1

您可以执行以下技巧,例如 而不是 sumbit 按钮只有普通按钮

<input type="button" value="Update" onClick="submit2forms();"/>

并单击此按钮调用下面的javascript方法

        <script language="javascript">
            function submit2forms() {
                document.getElementById("form1").submit();
                document.getElementById("form2").submit();
            }
        </script>
于 2012-06-22T11:38:39.237 回答