ฉันได้อ่านหัวข้อที่คล้ายกันแล้ว ชอบสิ่งนี้ และ อันนั้น แต่มัน ไม่ได้ช่วยแก้ไขปัญหาของฉัน
ฉันสร้างเว็บแอป JavaEE mvc อย่างง่าย หน้า jsp มีแบบฟอร์มที่มีช่องข้อความสองช่องและปุ่มสองปุ่ม ช่องข้อความแรกสำหรับป้อนรหัส ช่องที่สองสำหรับป้อนชื่อ ขึ้นอยู่กับว่าคลิกปุ่มใด Servlet จะกำหนดเส้นทางไปยังวิธีการที่เหมาะสม (ค้นหาด้วย ID หรือค้นหาตามชื่อ) ค้นหาด้วยวิธีการ id ทำงานได้อย่างถูกต้อง และฉันเห็นเส้นทางต่อไปนี้ในแถบที่อยู่: http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=1&command=Search_ID&employeeName=
อย่างไรก็ตาม มีปัญหากับวิธีการค้นหาตามชื่อ มันไม่แสดงผลใดๆ นี่คือสิ่งที่ฉันเห็นในแถบที่อยู่: http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=&employeeName=ann&command=Search_Name
ฉันเดาว่าปัญหาคือในทั้งสองกรณีจะได้รับพารามิเตอร์ของทั้งสองช่องข้อความ ("textEmployeeId" และ "employeeName") ฉันจะทำให้ประมวลผลทั้งสองอินพุตแยกกันในรูปแบบเดียวกันได้อย่างไร อาจมีเหตุผลอื่นสำหรับปัญหาที่ฉันไม่เห็นใช่ไหม
<form class="form-style" name="form1">
<label>ID:
<input type="text" name="textEmployeeId" value="${tempEmployee.id}" />
</label>
<input type="submit" name="command" value="Search_ID">
<br>
<br>
<label>Name:
<input type="text" name="employeeName" value="${tempEmployee.name}" />
</label>
<input type="submit" name="command" value="Search_Name">
<br>
<br>
</form>
คอนโทรลเลอร์Servlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String theCommand = request.getParameter("command");
if (theCommand == null) {
theCommand = "EMPLOYEE_LIST";
}
// route to the appropriate method
switch (theCommand) {
case "EMPLOYEE_LIST":
listEmployees(request, response);
break;
case "Search_ID":
searchById(request, response);
break;
case "Search_Name":
searchByName(request, response);
break;
default:
listEmployees(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void searchByName(HttpServletRequest request, HttpServletResponse response) throws Exception {
String nameString = request.getParameter("employeeName");
List<Employee> namedEmployees = employeeDbUtil.searchEmployees(nameString);
request.setAttribute("EMPLOYEES", namedEmployees);
RequestDispatcher dispatcher = request.getRequestDispatcher("/search-by-name.jsp");
dispatcher.forward(request, response);
}
private void searchById(HttpServletRequest request, HttpServletResponse response) throws Exception {
String textString = request.getParameter("textEmployeeId");
Employee theEmployee = employeeDbUtil.getEmployeeById(textString);
request.setAttribute("THE_EMPLOYEE", theEmployee);
RequestDispatcher dispatcher = request.getRequestDispatcher("/show-employee.jsp");
dispatcher.forward(request, response);
}
}
อัปเดต: ฉันได้รับ NullPointerException สำหรับวิธี searchByName อาจมีปัญหากับวิธีนี้จากคลาส DAO ที่ถูกเรียกจาก ControllerServlet หรือไม่
public List<Employee> searchEmployees(String employeeName) throws Exception {
List<Employee> employeeList = new ArrayList();
Connection myConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
employeeName += "%";
preparedStatement = myConnection.prepareStatement("SELECT * FROM employees WHERE LOWER(name) like LOWER(?)");
preparedStatement.setString(1, employeeName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Employee tempEmployee = resultSetToEmployee(resultSet);
employeeList.add(tempEmployee);
}
return employeeList;
}
finally {
DbExceptions.close(resultSet);
DbExceptions.close(preparedStatement);
DbExceptions.close(myConnection);
}
}
search-by-name.jsp
หรือไม่ ดูเหมือนว่าโฟลว์การดำเนินการเซิร์ฟเล็ตของคุณออกจากแอตทริบิวต์คำสั่ง และเพียงเข้าถึงแอตทริบิวต์อื่นๆ ตามคำสั่ง - person Zachary Craig   schedule 13.12.2016/show-employee.jsp
ที่แสดงผลลัพธ์ที่คาดหวัง แต่เมื่อฉันกดปุ่ม Search_Name ฉันได้รับ NullPointerException และมันไม่เปลี่ยนเส้นทางฉันไปยังหน้าsearch-by-name.jsp
มีเพียงหน้าเบราว์เซอร์สีขาวว่างเปล่า - person Ivan T   schedule 14.12.2016searchEmployees(String employeeName)
จากคลาส DAO อย่างที่คุณเห็นมีบรรทัดConnection myConnection = null;
แต่ฉันไม่ได้รับการเชื่อมต่อจากแหล่งข้อมูล ดังนั้นเมธอดจึงส่งคืนรายการว่าง การเพิ่มบรรทัดต่อไปนี้myConnection = dataSource.getConnection();
ลงในบล็อก try ช่วยแก้ปัญหาได้ - person Ivan T   schedule 15.12.2016