gpt4 book ai didi

java - 在 Spring MVC、CrudRepository、Thymeleaf 中搜索(通过文本字段和按钮)

转载 作者:行者123 更新时间:2023-11-30 06:58:50 25 4
gpt4 key购买 nike

我想创建一个简单的按钮来通过 CrudRepository 的方法进行搜索,并在网站上显示记录。我是初学者,所以这对你来说似乎太微不足道了,但我仍然在寻求建议:)

StudentRepository.java

public interface StudentRepository extends CrudRepository<Student, Integer> {
Iterable<Student> findBySurname(String surname);
}

StudentService.java

public interface StudentService {
Iterable<Student> listStudentsBySurname(String surname);
}

StudentServiceImpl.java

@Service
public class StudentServiceImpl implements StudentService {
private StudentRepository studentRepository;

@Autowired
public void setStudentRepository(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}

@Override
public Iterable<Student> listStudentsBySurname(String surname) {
return studentRepository.findBySurname(surname);
}
}

students.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

<title></title>

<!--/*/ <th:block th:include="fragments/headerincStudent :: head"></th:block> /*/-->
</head>
<body>
<div class="container">
<!--/*/ <th:block th:include="fragments/headerStudent :: header"></th:block> /*/-->

<h2>Search for students</h2>
<form th:object="${student}" th:action="@{/students}" method="get">
<input type="text" name="search" id="search" th:value="${search}"/>
<input type="submit" value="Search"/>
<div th:if="${not #lists.isEmpty(search)}">
<h2>Students List</h2>
<table class="table table-striped">
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
</tr>
<tr th:each="student: ${search}">
<td th:text="${student.idStudent}"><a href="/student/${student.idStudent}">idStudent</a></td>
<td th:text="${student.name}">name</td>
<td th:text="${student.surname}">surname</td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>

StudentController.java

@Controller
public class StudentController {
private StudentService studentService;

@Autowired
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}

@RequestMapping(value = "students/{surname}", method = RequestMethod.GET)
public String showStudentBySurname(@PathVariable String surname, Model model) {
model.addAttribute("search", studentService.listStudentsBySurname(surname));
return "students";
}
}

我设法使用本地数据库进行简单的 CRUD。我已经直接在网站上通过 findAll 方法查看了学生列表,但现在我想对带有按钮的文本字段执行此操作,但我不知道下一步要做什么。

最佳答案

您需要更改它:

@RequestMapping(value = "students/{surname}", method = RequestMethod.GET)
public String showStudentBySurname(@PathVariable String surname, Model model) {
model.addAttribute("search", studentService.listStudentsBySurname(surname));
return "students";
}

至:

@RequestMapping(value = "students", method = RequestMethod.GET)
public String showStudentBySurname(@RequestParam (value = "surname", required = false) String surname, Model model) {
model.addAttribute("search", studentService.listStudentsBySurname(surname));
return "students";
}

因为您从表单发送参数。 @PathVariable如果您想通过链接获取搜索结果(例如 <a href="/students/surname">Surname </a> ),则使用

关于java - 在 Spring MVC、CrudRepository、Thymeleaf 中搜索(通过文本字段和按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314724/

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