gpt4 book ai didi

java - 使用另一个表的数据填充 thymeleaf 下拉列表

转载 作者:行者123 更新时间:2023-12-02 00:50:18 25 4
gpt4 key购买 nike

我是 spring-boot 的新手。在我的系统中,有两个名为 subject,course 的模型(两个数据库具有相同的名称并通过外键 course_id 连接)。我需要能够在 addSubject thymeleaf 表单的下拉列表中选择类(class)名称。有人可以告诉我该怎么做

Subject.DAO 文件

@Service
public class SubjectDAO {

@Autowired
SubjectRepository subjectRepository;

//to save a subject
public Subject save(Subject subject){
return subjectRepository.save(subject);
}

//to search all subjects
public List<Subject> findAll(){
return subjectRepository.findAll();
}

//get a subject by id
public Subject findById(Long id){
return subjectRepository.findById(id).orElse(null);
}


//delete a subject
public void delete(Long id){
subjectRepository.deleteById(id);
}


}

类(class).DAO文件

@Service
public class CourseDAO {

@Autowired
CourseRepository courseRepository;

//to save a course
public Course save(Course course){
return courseRepository.save(course);
}

//to search all courses
public List<Course> findAll(){
return courseRepository.findAll();
}

//get a course by id
public Course findById(Long id){
return courseRepository.findById(id).orElse(null);
}


//delete a course
public void delete(Long id){
courseRepository.deleteById(id);
}


}

主题 Controller

@Controller
public class SubjectController {
@Autowired
private SubjectDAO subjectDAO;

@RequestMapping("/subject")
public String viewHomePage(Model model){
List<Subject> subjectDetails= subjectDAO.findAll();
model.addAttribute("subjectDetails",subjectDetails);
return "subject";
}

@RequestMapping("/subject/new")
public String addSubject(Model model){
Subject subject =new Subject();
model.addAttribute("subject",subject);
return "addSubject";
}

@RequestMapping(value="/subject/save",method= RequestMethod.POST)
public String saveCourse(@ModelAttribute("subject") Subject subject){
subjectDAO.save(subject);
return "redirect:/subject";
}

@RequestMapping("/subject/edit/{id}")
public ModelAndView updateSubjcet(@PathVariable(name="id")Long id){
ModelAndView mav=new ModelAndView(("updateSubject"));

Subject subject=subjectDAO.findById(id);
mav.addObject("subject",subject);
return mav;
}

@RequestMapping("/subject/delete/{id}")
public String deleteProduct(@PathVariable(name="id") Long id){
subjectDAO.delete(id);
return "redirect:/subject";
}
}

主题 html 文件

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Adding a Subject</title>
</head>
<body>
<div align="center">
<h1>Add a new Subject</h1>
<br/>
<form action="#" th:action="@{/subject/save}" th:object="${subject}" method="post">
<table border="0" cell[adding="10">
<tr>
<td>Subject code:</td>
<td><input type="text" th:field="*{course_code}" /></td>
</tr>
<tr>
<td>Subject Name:</td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td>Course:</td>
<td>
<select>
<option value=""></option>

</select>
Z
</td>
</tr>
<tr>
<td colspan="2"><button type="submit">Save</button></td>
</tr>
</table>
</form>
</div>

</body>
</html>

最佳答案

所以你想在主题 html 中加载类(class)组合

您需要修改主题 Controller

@RequestMapping("/subject")
public String viewHomePage(Model model){
List<Subject> subjectDetails= subjectDAO.findAll();
List<Course> courseDetail= courseDAO.findAll();
model.addAttribute("subjectDetails",subjectDetails);
model.addAttribute("courses",courseDetail);
return "subject";
}

在 HTML 中

<tr>
<td>Course:</td>
<td>
<select th:field="*{course_code}">
<option value="">Choose..</option>
<option th:each="course: ${courses}" th:value="${course.id}" th:text="${course.name}" />
</select>
</td>
</tr>
<tr>

编辑1:

我认为您正在加载addSubject.html

在这种情况下,您需要修改 addSubject Controller

@RequestMapping("/subject/new")
public String addSubject(Model model){
Subject subject =new Subject();
model.addAttribute("subject",subject);
List<Course> courseDetail= courseDAO.findAll();
model.addAttribute("courses",courseDetail);
return "addSubject";
}

编辑2:

根据你在 Git 中的代码,我可以看到

@Autowired
private SubjectDAO subjectDAO;
private CourseDAO courseDAO;

应该是

 @Autowired
private SubjectDAO subjectDAO;

@Autowired
private CourseDAO courseDAO;

关于java - 使用另一个表的数据填充 thymeleaf 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57865405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com