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

15,083

You need change it:

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

to:

@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";
}

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

Share:
15,083
user7337867
Author by

user7337867

Updated on June 05, 2022

Comments

  • user7337867
    user7337867 almost 2 years

    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";
        }
    }
    

    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.

  • user7337867
    user7337867 over 7 years
    Thanks, but it still doesn't work :/ [Before (start app)][1] [After, when I put a surname, but in the database there is a surname like that][2] [1]: i.stack.imgur.com/V8qyK.jpg [2]: i.stack.imgur.com/jMliS.jpg
  • Vit Stukalov
    Vit Stukalov over 7 years
    Just change value = "surname" to value = "search" in @RequestParam