2012-06-21 35 views
2

你好傢伙有可能在單個jsp上有多個表單,也可以用一個按鈕嗎?jsp上的spring mvc多表單

這裏是我的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"); 
    } 

回答

0

是的,你可以讓你的按鈕提交您的兩種形式。但你已經用ajax做了。

-1

你可以做以下伎倆像 而是透過按鈕都只有正常的按鈕

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

和JavaScript的方法下面這個按鈕調用的點擊爲

 <script language="javascript"> 
      function submit2forms() { 
       document.getElementById("form1").submit(); 
       document.getElementById("form2").submit(); 
      } 
     </script> 
+0

我認爲這是行不通的。 – jddsantaella

+0

我已經試過這個選項,並且只爲第一個提交第二個未執行的工作。 – user1331582

4

你可以有多種形式在JSP中,但不能同時發送。

您應該混合兩種表單和兩種操作,檢索操作/控制器中的所有信息並保存電話和用戶信息。另一種選擇是使用Ajax發送一個表單並像往常一樣發送其他表單。

順便問一下,你的問題與Spring無關。

+0

如果你想同時發送這兩個選項,創建一個包含兩個當前表單信息的表單可能更容易。否則,請使用2個不同的請求和按鈕 – Thomas

0

您可以通過使用document.forms [i]和每個單獨的形式調用網頁上的表格需要循環提交

0

你唯一的選擇是做AJAX,你必須認識到,如果你控制方法將在第一次提交您的第二次提交後通過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--) 
      } 
     } 
    }); 

}