forms - jsp上的spring mvc多种形式

标签 forms jsp model-view-controller

大家好,是否可以在单个 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>

和 Controller

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");
     }

最佳答案

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

您应该混合使用两种表单和两种操作,检索操作/ Controller 中的所有信息并保存电话和用户信息。另一种选择是使用 Ajax 发送其中一个表单,然后照常发送另一个表单。

顺便说一下,你的问题与Spring无关。

关于forms - jsp上的spring mvc多种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11141648/

相关文章:

Javascript 动态生成选择菜单,php 在 POST 中看不到

javascript - 选择决定打开哪个表单操作

html - 为什么第一个元素在我的 Rails 多选中总是空白,使用嵌入式数组?

java - 如何在通过java中的jsp显示的Web驱动程序中断言java异常?

java - 错误请求 400

forms - 如何使用 pwsh 中的新 param -form 登录网站

javascript - 如何将文本字段值从 jsp 传递到 spring Controller 类,反之亦然

java - Spring MVC,两个按钮与一个表单一起工作,它们如何协同工作?

php - 这是在 PHP mvc 中缓存查询和构建页面的可接受方式吗?

javascript - Ng-model 没有按预期工作?