title: Spring MVC - how to update row in database using JSP table?
body: <p>I got problem with update entered values in jsp table, I don't know where I should put <code>form action</code> and <code>c:forEach</code> in order to everything will be comaptible. Where is the problem?</p>
  
  <p>In this case entered values receives <code>null</code>, and after click "edit" every row is being empty:</p>
  
  <pre><code>&lt;div class="forms"&gt;
      &lt;div class="selectr"&gt;
      &lt;p style="font-size: 13"&gt; 
      &lt;table border="1"&gt;
          &lt;tr style="font-size: 13"&gt;
              &lt;td&gt;ID&lt;/td&gt;
              &lt;td&gt;First name&lt;/td&gt;
              &lt;td&gt;Last name&lt;/td&gt;
              &lt;td&gt;Gender&lt;/td&gt;
              &lt;td&gt;Position&lt;/td&gt;
              &lt;td&gt;Salary&lt;/td&gt;
              &lt;td&gt;Phone number&lt;/td&gt;
              &lt;td&gt;Address&lt;/td&gt;
              &lt;td&gt;Action&lt;/td&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${employees}" var="employee"&gt;
              &lt;tr style="font-size: 10"&gt;
                  &lt;td&gt;&lt;input disabled style="width: 17px" type="text" name="id" value="${employee.id}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 75px" type="text" name="name" value="${employee.name}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 60px"  type="text" name="gender" value="${employee.gender}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 80px" type="text" name="position" value="${employee.position}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 60px" type="text" name="salary" value="${employee.salary}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 160px" type="text" name="address" value="${employee.address}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;form action="/VirtualClinic/employeelist.html?editEmployee=${employee.id}"  method="POST"&gt;&lt;input type="submit" value="Edit"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/c:forEach&gt;
      &lt;/table&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>In this case entered values is not null (I check it with <code>System.out.println(employee.getName()</code> but still they dont want to be updated, because after click updated value get back to default value from database):</p>
  
  <pre><code> &lt;form action="/VirtualClinic/employeelist.html?editEmployee="   method="POST"&gt;
   &lt;div class="forms"&gt;
      &lt;div class="selectr"&gt;
      &lt;p style="font-size: 13"&gt; 
      &lt;table border="1"&gt;
          &lt;tr style="font-size: 13"&gt;
              &lt;td&gt;ID&lt;/td&gt;
              &lt;td&gt;First name&lt;/td&gt;
              &lt;td&gt;Last name&lt;/td&gt;
              &lt;td&gt;Gender&lt;/td&gt;
              &lt;td&gt;Position&lt;/td&gt;
              &lt;td&gt;Salary&lt;/td&gt;
              &lt;td&gt;Phone number&lt;/td&gt;
              &lt;td&gt;Address&lt;/td&gt;
              &lt;td&gt;Action&lt;/td&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${employees}" var="employee"&gt;
              &lt;tr style="font-size: 10"&gt;
                  &lt;td&gt;&lt;input disabled style="width: 17px" type="text" name="id" value="${employee.id}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 75px" type="text" name="name" value="${employee.name}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 60px"  type="text" name="gender" value="${employee.gender}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 80px" type="text" name="position" value="${employee.position}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 60px" type="text" name="salary" value="${employee.salary}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input style="width: 160px" type="text" name="address" value="${employee.address}"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="submit" value="Edit"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/c:forEach&gt;
      &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>DAO:</p>
  
  <pre><code>public void updateEmployee(Employee employee) {
      String query = "update virtualclinic.employee SET name=?, lastname=?, gender=?,"
              + "position=?, salary=?, phonenumber=?, address=? WHERE idemployee=?";
      Connection con = null;
      PreparedStatement ps = null;
      try{
          con = dataSource.getConnection();
          ps = con.prepareStatement(query);
          ps.setString(1, employee.getName());
          ps.setString(2, employee.getLastName());
          ps.setString(3, employee.getGender());
          ps.setString(4, employee.getPosition());
          ps.setString(5, employee.getSalary());
          ps.setString(6, employee.getPhoneNumber());
          ps.setString(7, employee.getAddress());
          ps.setString(8, employee.getId());
  
          int out = ps.executeUpdate();                    
  
      }catch(SQLException e){
          e.printStackTrace();
      }finally{
          try {
              ps.close();
              con.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>public void updateEmployee(Employee employee) {
      ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
      employeeDAO = ctx.getBean("employeeDAO", EmployeeDAOImpl.class);            
  
      employeeDAO.updateEmployee(employee);
  
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>    @RequestMapping(value = "/employeelist.html", method = RequestMethod.POST)
  public ModelAndView deleteEmployee(Model model, @ModelAttribute("employee") Employee employee, @RequestParam String editEmployee) throws SQLException {
  
      setAppContext();
  
      clinicService.updateEmployee(employee);
  
      System.out.println(employee.getName());
  
      List&lt;Employee&gt; employees = clinicService.getAllEmployees();
      model.addAttribute("employees", employees);
  
      ModelAndView mstaff = new ModelAndView("EmployeeList");
      return mstaff;
  
  }
  </code></pre>