2016-02-18 37 views
3

我在jsp表中更新輸入的值時遇到問題,我不知道我應該在哪裏放置form actionc:forEach以便一切都會變得易於理解。哪裏有問題?Spring MVC - 如何使用JSP表更新數據庫中的行?

在這種情況下,輸入的數值接收null,之後點擊「編輯」中的每一行是空:

<div class="forms"> 
    <div class="selectr"> 
    <p style="font-size: 13"> 
    <table border="1"> 
     <tr style="font-size: 13"> 
      <td>ID</td> 
      <td>First name</td> 
      <td>Last name</td> 
      <td>Gender</td> 
      <td>Position</td> 
      <td>Salary</td> 
      <td>Phone number</td> 
      <td>Address</td> 
      <td>Action</td> 
     </tr> 
     <c:forEach items="${employees}" var="employee"> 
      <tr style="font-size: 10"> 
       <td><input disabled style="width: 17px" type="text" name="id" value="${employee.id}"></td> 
       <td><input style="width: 75px" type="text" name="name" value="${employee.name}"></td> 
       <td><input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"></td> 
       <td><input style="width: 60px" type="text" name="gender" value="${employee.gender}"></td> 
       <td><input style="width: 80px" type="text" name="position" value="${employee.position}"></td> 
       <td><input style="width: 60px" type="text" name="salary" value="${employee.salary}"></td> 
       <td><input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"></td> 
       <td><input style="width: 160px" type="text" name="address" value="${employee.address}"></td> 
       <td><form action="/VirtualClinic/employeelist.html?editEmployee=${employee.id}" method="POST"><input type="submit" value="Edit"/></td> 
      </tr> 
      </c:forEach> 
    </table> 
    </div> 

在這種情況下輸入的值不爲空(我System.out.println(employee.getName()檢查,但仍然他們不想更新,因爲點擊更新後的值從數據庫恢復到默認值):

<form action="/VirtualClinic/employeelist.html?editEmployee=" method="POST"> 
<div class="forms"> 
    <div class="selectr"> 
    <p style="font-size: 13"> 
    <table border="1"> 
     <tr style="font-size: 13"> 
      <td>ID</td> 
      <td>First name</td> 
      <td>Last name</td> 
      <td>Gender</td> 
      <td>Position</td> 
      <td>Salary</td> 
      <td>Phone number</td> 
      <td>Address</td> 
      <td>Action</td> 
     </tr> 
     <c:forEach items="${employees}" var="employee"> 
      <tr style="font-size: 10"> 
       <td><input disabled style="width: 17px" type="text" name="id" value="${employee.id}"></td> 
       <td><input style="width: 75px" type="text" name="name" value="${employee.name}"></td> 
       <td><input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"></td> 
       <td><input style="width: 60px" type="text" name="gender" value="${employee.gender}"></td> 
       <td><input style="width: 80px" type="text" name="position" value="${employee.position}"></td> 
       <td><input style="width: 60px" type="text" name="salary" value="${employee.salary}"></td> 
       <td><input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"></td> 
       <td><input style="width: 160px" type="text" name="address" value="${employee.address}"></td> 
       <td><input type="submit" value="Edit"/></td> 
      </tr> 
      </c:forEach> 
    </table> 
    </div> 
</div> 
</form> 

DAO:

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

} 

服務:

public void updateEmployee(Employee employee) { 
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml"); 
    employeeDAO = ctx.getBean("employeeDAO", EmployeeDAOImpl.class);    

    employeeDAO.updateEmployee(employee); 

} 

控制器:

@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<Employee> employees = clinicService.getAllEmployees(); 
    model.addAttribute("employees", employees); 

    ModelAndView mstaff = new ModelAndView("EmployeeList"); 
    return mstaff; 

} 
+2

示例:http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/ – dit

+0

更好地使用Ajax而不是單獨更新每行。 否則,你必須改變你的HTML設計,因爲表單動作寫在表格的結尾,但表單元素應該在表單標籤內。 –

+0

使用ajax更新行或創建編輯按鈕,該按鈕顯示另一頁上的現有數據,然後使用單個表單更新數據 –

回答

0

假設這是在項目中的第一個JSP。另外,我沒有看到您的tld導入,但EL和JSTL問題最常見的原因之一是您使用的JSP版本,您使用的JSTL版本以及您擁有的版本之間的配置不匹配在部署描述符(web.xml)中聲明您的Web應用程序。

對於JSP 2.1容器(例如Tomcat 6),您應該使用JSTL 1.2,並且應該使用Servlets 2.5 XML Schema將您的Web應用程序聲明爲Servlets 2.5 Web應用程序。

對於JSP 2.0容器(例如Tomcat 5),您應該使用JSTL 1.1,並且應該使用Servlets 2.4 XML Schema將您的Web應用程序聲明爲Servlets 2.4 Web應用程序。

相關問題