我在jsp表中更新輸入的值時遇到問題,我不知道我應該在哪裏放置form action
和c: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;
}
示例:http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/ – dit
更好地使用Ajax而不是單獨更新每行。 否則,你必須改變你的HTML設計,因爲表單動作寫在表格的結尾,但表單元素應該在表單標籤內。 –
使用ajax更新行或創建編輯按鈕,該按鈕顯示另一頁上的現有數據,然後使用單個表單更新數據 –