title: Search (via text field and button) in Spring MVC, CrudRepository, Thymeleaf
body: <p>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 :)</p>
  
  <p><strong>StudentRepository.java</strong></p>
  
  <pre><code>public interface StudentRepository extends CrudRepository&lt;Student, Integer&gt; {
      Iterable&lt;Student&gt; findBySurname(String surname);
  }
  </code></pre>
  
  <p><strong>StudentService.java</strong></p>
  
  <pre><code>public interface StudentService {
      Iterable&lt;Student&gt; listStudentsBySurname(String surname);
  }
  </code></pre>
  
  <p><strong>StudentServiceImpl.java</strong></p>
  
  <pre><code>@Service
  public class StudentServiceImpl implements StudentService {
      private StudentRepository studentRepository;
  
      @Autowired
      public void setStudentRepository(StudentRepository studentRepository) {
          this.studentRepository = studentRepository;
      }
  
      @Override
      public Iterable&lt;Student&gt; listStudentsBySurname(String surname) {
          return studentRepository.findBySurname(surname);
      }
  }
  </code></pre>
  
  <p><strong>students.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head lang="en"&gt;
  
      &lt;title&gt;&lt;/title&gt;
  
      &lt;!--/*/ &lt;th:block th:include="fragments/headerincStudent :: head"&gt;&lt;/th:block&gt; /*/--&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
      &lt;!--/*/ &lt;th:block th:include="fragments/headerStudent :: header"&gt;&lt;/th:block&gt; /*/--&gt;
  
      &lt;h2&gt;Search for students&lt;/h2&gt;
      &lt;form th:object="${student}" th:action="@{/students}" method="get"&gt;
          &lt;input type="text" name="search" id="search" th:value="${search}"/&gt;
          &lt;input type="submit" value="Search"/&gt;
          &lt;div th:if="${not #lists.isEmpty(search)}"&gt;
              &lt;h2&gt;Students List&lt;/h2&gt;
              &lt;table class="table table-striped"&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Id&lt;/th&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Surname&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;tr th:each="student: ${search}"&gt;
                      &lt;td th:text="${student.idStudent}"&gt;&lt;a href="/student/${student.idStudent}"&gt;idStudent&lt;/a&gt;&lt;/td&gt;
                      &lt;td th:text="${student.name}"&gt;name&lt;/td&gt;
                      &lt;td th:text="${student.surname}"&gt;surname&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>StudentController.java</strong></p>
  
  <pre><code>@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";
      }
  }
  </code></pre>
  
  <p>I managed to do simple CRUD with my local database. I have done viewing the list of students by method <em>findAll</em> directly on the website, but now I want do it for text field with button, but I don't know what next.</p>