當我嘗試通過運行我的應用程序來測試它時,導航到 admin.html 頁面。我在表格中填寫了名字、姓氏和電子郵件的值。單擊提交按鈕時,會出現“列電子郵件不能為空”的錯誤。為簡潔起見,我排除了 getter、setter、contructors 等代碼。這是我的 admin.html 頁面,其中有一個表單,用于將值發布到我的 api,其中的值用于創建員工對象<form role="form" action="api/employees/create" method="post"> <div class="form-group"> <label for="firstName">First Name</label> <input type="text" class="form-control" id="firstName" placeholder="Enter first name"> </div> <div class="form-group"> <label for="lastName">Last Name</label> <input type="text" class="form-control" id="lastName" placeholder="Enter last name"> </div> <div class="form-group"> <label for="email">Email</label> <input type="text" class="form-control" id="email" placeholder="Enter email"> </div> <button type="submit" class="btn btn-success btn-block">Create</button></form>這是我在 EmployeeAPI.java 類中的 POST 方法,我在其中處理帖子并使用從表單傳入的值創建一個對象,并嘗試保留這個新對象@POST@Path("create")@Consumes(MediaType.APPLICATION_FORM_URLENCODED)@Produces(MediaType.TEXT_HTML)public Response createEmployee(@FormParam(value = "firstName") String firstName, @FormParam(value = "lastName") String lastName, @FormParam(value = "email") String email) { SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.getCurrentSession(); URI location; try{ session.getTransaction().begin(); Employee newEmployee = new Employee(); newEmployee.setFirstName(firstName); newEmployee.setLastName(lastName); newEmployee.setEmail(email); session.persist(newEmployee); session.getTransaction().commit(); session.close(); location = new URI("http://localhost:8080/index.html"); return Response.temporaryRedirect(location).build(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } return null;}
添加回答
舉報
0/150
提交
取消