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

Search (via text field and button) in Spring MVC, CrudRepository, Thymeleaf

我想创建一个简单的按钮来通过 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";
}

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

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

实际问题是您使用不存在的名为 surname 的参数按姓氏搜索学生,同时您想使用“search”搜索,即在 HTML 文件中输入的搜索名称。