我想为员工管理系统创建一个简单的crud应用程序。当我在浏览器中单击添加按钮时,我会被重定向到localhost:9090/employees/new,但是当我单击提交按钮而不是重定向回localhost:9090/employees时,我会得到一个错误(错误请求-400),这可能是什么问题?
这是我的员工控制器
package com.example.crudempdep.controller;
import com.example.crudempdep.model.Employee;
import com.example.crudempdep.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employees")
public String getAllEmployees(Model model) {
List<Employee> employees = employeeService.getAllEmployees();
model.addAttribute("employees", employees);
return "employees";
}
@GetMapping("/employees/new")
public String createEmployee(Model model) {
model.addAttribute("employee", new Employee());
return "create_employee";
}
@PostMapping("/employees")
public String addEmployee(@ModelAttribute("employee") Employee employee) {
employeeService.saveEmployee(employee);
return "redirect:http://localhost:9090/employees";
}
@PutMapping("/employees/{id}")
public void updateEmployeeById(@PathVariable Long id, @RequestBody Employee employee) {
Employee e = employeeService.getEmployeeById(id);
if (e != null) {
e.setFname(employee.getFname());
e.setLname(employee.getLname());
e.setDepartment(employee.getDepartment());
employeeService.saveEmployee(e);
}
}
@DeleteMapping("/employees/{id}")
public void deleteEmployeeById(@PathVariable Long id) {
employeeService.deleteEmployeeById(id);
}
}
这是我的博客。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Employee Management System</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="#">Employee Management System</a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" th:href="@{/employees}">Employee Management</a>
</li>
<li class="nav-item">
<a class="nav-link" th:href="@{/departments}">Department Management</a>
</li>
</ul>
</div>
</nav>
<div class ="container">
<div class = "row">
<h1> List Students </h1>
</div>
<div class = "row">
<div class = "col-lg-3">
<a th:href = "@{/employees/new}" class = "btn btn-primary btn-sm mb-3"> Add Student</a>
</div>
</div>
<table class = "table table-striped table-bordered">
<thead class = "table-dark">
<tr>
<th> Employee First Name</th>
<th> Employee Last Name</th>
<th> Employee Department</th>
<th> Actions </th>
</tr>
</thead>
<tbody>
<tr th:each = "employee: ${employees}">
<td th:text = "${employee.fname}"></td>
<td th:text = "${employee.lname}"></td>
<td th:text = "${employee.department}"></td>
<td>
<a th:href = "@{/employees.html/edit/{id}(id=${employee.id})}"
class = "btn btn-primary">Update</a>
<a th:href = "@{/employees.html/delete/{id}(id=${employee.id})}"
class = "btn btn-danger">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
这是create_employee.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="ISO-8859-1">
<title>Employee Management System</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="#">Employee Management System</a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" th:href="@{/employees}">Employee Management</a>
</li>
</ul>
</div>
</nav>
<br>
<br>
<div class = "container">
<div class = "row">
<div class ="col-lg-6 col-md-6 col-sm-6 container justify-content-center card">
<h1 class = "text-center"> Create New Employee </h1>
<div class = "card-body">
<form th:action="@{/employees}" th:object="${employee}" method="POST">
<div class ="form-group">
<label> Employee First Name </label>
<label>
<input
type = "text"
name = "fname"
th:field = "*{fname}"
class = "form-control"
placeholder="Enter Employee First Name"
/>
</label>
</div>
<div class ="form-group">
<label> Employee Last Name </label>
<label>
<input
type = "text"
name = "lname"
th:field = "*{lname}"
class = "form-control"
placeholder="Enter Employee Last Name"
/>
</label>
</div>
<div class ="form-group">
<label> Employee Department </label>
<label>
<input
type = "text"
name = "department"
th:field = "*{department}"
class = "form-control"
placeholder="Enter Employee Department"
/>
</label>
</div>
<div class = "box-footer">
<button type="submit" class = "btn btn-primary">
Submit
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
请注意,我将服务器端口设置为9090,这就是我硬编码重定向的原因,但它仍然不起作用
1条答案
按热度按时间dzhpxtsq1#
您应该用途:
而不是
在员工的岗位方法中