spring mvc multiple form on jsp

12,108

You can have multiple forms in a JSP, but you can NOT send both at the same time.

You should mix both forms and both actions, retrieve all information in the action/controller and save phone and user information. Another options would be to use Ajax to send one of the form and send the other as usually.

By the way, your problem has nothing to do with Spring.

Share:
12,108
user1331582
Author by

user1331582

Updated on June 04, 2022

Comments

  • user1331582
    user1331582 almost 2 years

    Hello guys is it possible to have multiple forms on single jsp and also with a single button?

    Here is my jsp page where i hava two forms, i know this way it is, it only save the second form.

    <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>
    

    and the controllers

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