我正在使用PostgreSQL作为数据库构建一个spring应用程序,现在已经尝试了thymeleaf,但我甚至不能制作@GetMapping。它不断给我一个白标签错误页面,标题中说明了错误。
控制器类别:
@Controller
@Slf4j
@SessionAttributes({"department"})
@RequestMapping("/department")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;
@Autowired
private EmployeeRepository employeeRepository;
@PostConstruct
public void initDB(){
Path path = Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "data.json");
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
List<Department> departments = null;
try {
departments = mapper.readValue(path.toFile(), new TypeReference<List<Department>>() {});
if(departments != null){
//Bi-Direktionalität
for(Department department : departments){
department.getEmployees().forEach(e -> e.setDepartment(department));
}
departmentRepository.saveAll(departments);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@GetMapping
public String getDepartmentInfo(Model model, @RequestParam("department") String departmentNumber){
Department department = departmentRepository.getDepartmentByDeptNo(departmentNumber);
updateModel(model, department);
return "departmentView";
}
public void updateModel(Model model, Department department){
model.addAttribute("department", department);
model.addAttribute("manager", department.getDeptManager());
model.addAttribute("employees", department.getEmployees());
}
}
我的HTML到目前为止:
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Departments</title>
</head>
<body>
<h1>Department</h1>
<form method="get" th:action="@{/department}">
<label for="choice"></label>
<select id="choice" name="department">
<option th:value="${department.deptNo}" th:text="${department.deptName}"></option>
</select>
</form>
</body>
</html>
就像我说的,我只从百里香叶开始,所以错误对一些人来说可能是显而易见的。
我尝试了YouTube教程,并在StackOverflow上搜索其他问题,但没有一个能解决我的问题。
1条答案
按热度按时间9avjhtql1#
默认情况下,您需要将department值作为必填项传递
也可以将department参数作为可选的request参数,如下所示