2012-06-21 4 views
2

안녕하세요 여러분은 단일 JSP에서 여러 개의 양식을 가질 수 있으며 하나의 버튼으로도 가능합니까?jsp에서 spring mvc multiple form

여기는 내 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

예, 당신은 버튼 양식을 모두 제출 할 수 있습니다. 하지만 당신은 아약스와 함께 해왔습니다.

당신은 대신 작성 버튼의 처럼 다음 트릭을 할 수
-1

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

같이 여러 형태를 가질 수

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

나는 그것이 작동하지 않습니다 생각합니다. – jddsantaella

+0

나는이 옵션을 시도해 본 적이 없으며 두 번째 질문은 결코 실행되지 않은 두 번째 질문 만 제출합니다 .. – user1331582

4

같은 자바 스크립트 방법 다음이 버튼을 호출 클릭에서만 정상 버튼이 있습니다 JSP에서는,하지만 당신은 동시에 둘 다 보낼 수 없습니다.

두 가지 양식과 두 가지 작업을 혼합하여 작업/컨트롤러의 모든 정보를 검색하고 전화 및 사용자 정보를 저장해야합니다. 또 다른 옵션은 Ajax를 사용하여 양식 중 하나를 보내고 다른 하나는 일반적으로 보내는 것입니다.

그런데 당신의 문제는 스프링과는 아무런 관련이 없습니다.

+0

두 가지를 동시에 보내려는 경우 두 가지 양식의 정보가 모두 포함 된 양식을 만드는 것이 더 쉽습니다. 그렇지 않으면 2 개의 다른 요청 및 버튼을 사용하십시오. – Thomas

0

당신은 당신의 유일한 옵션은 Ajax 용하고있다

0

를 제출 개별적으로 호출하는 경우 - document.forms [i]는 각각의 양식을 사용하여 웹 페이지에있는 양식을 통해 루프 필요, 당신은 깨닫게해야 당신이 컨트롤러 방법 경우 첫 번째 제출 후 페이지를 렌더링하고 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--) 
      } 
     } 
    }); 

은}

관련 문제