我在 jsp 表中更新输入值时遇到问题,我不知道我应该把 form action
和 c:forEach
放在哪里,以便一切都兼容.问题出在哪里?
在这种情况下,输入的值接收到 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>
道:
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);
}
Controller :
@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;
}
最佳答案
假设这是您项目中的第一个 JSP。此外,我没有看到您的 tld 导入,但 EL 和 JSTL 问题的最常见原因之一是您正在使用的 JSP 版本、您使用的 JSTL 版本以及您使用的方式之间的配置不匹配在部署描述符 (web.xml) 中声明您的 Web 应用程序。
对于 JSP 2.1 容器(例如 Tomcat 6),您应该使用 JSTL 1.2,并且您应该使用 Servlets 2.5 XML 架构将您的 Web 应用程序声明为 Servlets 2.5 Web 应用程序。
对于 JSP 2.0 容器(例如 Tomcat 5),您应该使用 JSTL 1.1,并且您应该使用 Servlets 2.4 XML 架构将您的 Web 应用程序声明为 Servlets 2.4 Web 应用程序。
关于java - Spring MVC - 如何使用 JSP 表更新数据库中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478440/