在Spring MVC,CrudRepository,Thymeleaf中搜索(通过文本字段和按钮) [英] Search (via text field and button) in Spring MVC, CrudRepository, Thymeleaf

查看:134
本文介绍了在Spring MVC,CrudRepository,Thymeleaf中搜索(通过文本字段和按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的按钮,通过CrudRepository的方法进行搜索,并在网站上显示记录。
我是一个初学者,所以对您来说似乎太琐碎了,但是我还是要征求意见:)

I want create a simple button to search, via methods of CrudRepository, and show records on the website. I'm a beginner, so it may seem too trivial to you, but I'm asking for advice nevertheless :)

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 查看了学生列表,但是现在我想对带有按钮的文本字段进行此操作,但是我不知道接下来要做什么。

I managed to do simple CRUD with my local database. I have done viewing the list of students by method findAll directly on the website, but now I want do it for text field with button, but I don't know what next.

推荐答案

您需要更改它:

@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>

Because you send parameters from the form. @PathVariable is used if you want get search result by link(for example <a href="/students/surname">Surname </a>)

这篇关于在Spring MVC,CrudRepository,Thymeleaf中搜索(通过文本字段和按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆