title: How to set up a search bar using Spring Boot, JPA and Thymeleaf
body: <p>So, I have a spring web app, it's a simple form to enter classes, lecturers, etc... Now I need a simple search bar for these classes which will return results which match the subject of a class.</p>
  
  <p>This is my code, and what I have done so far.</p>
  
  <p>Lecture.java</p>
  
  <pre><code>@Data
  @Entity
  @Table(name="Lecture")
  @NoArgsConstructor
  public class Lectureimplements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Valid
      @OneToOne(targetEntity=Lecturer.class, cascade=CascadeType.ALL)
      @JoinTable(
          name="Lecture_Lecturer",
          joinColumns = @JoinColumn(name = "lecture"),
          inverseJoinColumns = @JoinColumn(name = "lecturer"))
      private Lecturer lecturer;
  
      @NotEmpty(message = "Subject is empty!")
      @Size(min = 2, max = 50, message = "Subject must be from 2 to 50 letters long!")
      @Column(name="subject")
      private String subject;
  
      @NotEmpty(message = "You haven't entered a short description!")
      @Size(min = 2, max = 150, message = "A short description must be from 2 to 150 letters long!")
      @Column(name="description")
      private String shortDescription;
  
      @NotNull(message = "You haven't picked a lecture type!")
      @Column(name="type")
      @Enumerated(EnumType.STRING)
      private LectureType lectureType;
  
      @Column(name="published")
      private Boolean published;
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name="creationDate")
      private Date creationDate;
  
  
      public enum LectureType {
  
          PRACTICE, PRESENTATION
      }
  }
  </code></pre>
  
  <p>LectureController.java</p>
  
  <pre><code>@Slf4j
  @Controller
  @RequestMapping
  @SessionAttributes({"types", "positions", "lectureList", "published"})
  public class LectureController {
  
      @Autowired
      LectureRepository lectureR;
  
      @Autowired
      LecturerRepository lecturerR;
  
      List&lt;Lecture&gt; lectureList = new ArrayList&lt;&gt;();
  
      @RequestMapping
      public String newLecture() {
  
          return "newLecture";
      }
  
      @GetMapping("/newLecture")
      public String showForm(Model model, Lecture lecture) {
  
          log.info("Filling data to show form.");
  
          model.addAttribute("lecture", new Lecture());
          model.addAttribute("types", Lecture.LectureType.values());
          model.addAttribute("positions", Lecturer.LecturerPositions.values());
          model.addAttribute("published", lecture.getPublished());
  
          return "newLecture";
      }
  
      @GetMapping("/allLectures")
      public String showLectures() {
  
          return "allLectures";
      }
  
      @GetMapping("/resetCounter")
      public String /resetCounter(SessionStatus status) {
  
          lectureList.clear();
          status.setComplete();
          return "redirect:/newLecture";
      }
  
      @PostMapping("/novoPredavanje")
      public String processForm(@Valid Lecture lecture, Errors errors, Model model) {
  
      log.info("Processing lecture: " + lecture);
  
      if(errors.hasErrors()) {
  
          log.info("Lecture has errors. Ending.");
  
          return "newLecture";
  
      } else {
  
          lectureList.add(lecture);
  
          model.addAttribute("numberOfLectures", lectureList.size());
  
          model.addAttribute("lecture", lecture);
  
          model.addAttribute("published", lecture.getPublished());
  
          model.addAttribute("lectureList", lectureList);
  
          log.info("Lecture successfully saved: " + lecture);
  
          return "output";
      }
  }
  
      @GetMapping("/lectureSearch")
      public String lectureSearch(Model model) {
  
          model.addAttribute("lecture", new Lecture());
  
          return "lectureSearch";
      }
  
      @PostMapping("/lectureSearch")
      public String lectureSearch(Lecture lecture, Model model, String subject) {
  
          List&lt;Lecture&gt; foundLectures = lectureR.findBySubject(subject);
          model.addAttribute("foundLectures", foundLectures);
  
          return "lectureSearch";
      }
  }
  </code></pre>
  
  <p>LectureRepository.java</p>
  
  <pre><code>@Repository
  public interface LectureRepository extends CrudRepository&lt;Lecture, Long&gt; {
  
      List&lt;Lecture&gt; findBySubject(String subject);
  }
  </code></pre>
  
  <p>lectureSearch.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/extras/spring-security"&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" &gt;
          &lt;link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.css}" &gt;
          &lt;title&gt;Lecture output&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;img th:src="@{~/images/picture.png}" /&gt;
  
          &lt;form action="/resetCounter"&gt;
              &lt;input type="submit" value="Reset counter" class="btn btn-warning" style="background-color: orange"/&gt;
          &lt;/form&gt;
  
          &lt;div sec:authorize="isAuthenticated()"&gt;
              &lt;a th:href="@{/newLecture}" class="btn btn-primary"&gt;Enter a new lecture&lt;/a&gt;
              &lt;a th:href="@{/svaPredavanja}" class="btn btn-primary"&gt;All lectures&lt;/a&gt;
              &lt;a th:href="@{/pretragaPredavanja}" class="btn btn-primary"&gt;Lecture search&lt;/a&gt;
          &lt;/div&gt;
  
          &lt;div sec:authorize="isAuthenticated()"&gt;
              &lt;form method="POST" th:action="@{/logout}"&gt;You are logged in as &lt;span sec:authentication="name"&gt;User&lt;/span&gt;.
                  &lt;input type="submit" value="Logout" class="btn btn-danger" /&gt;
              &lt;/form&gt;
          &lt;/div&gt;
  
          &lt;div&gt;&lt;h3&gt;Lecture search&lt;/h3&gt;&lt;/div&gt;
          &lt;form th:object="${lecture}" method="post"&gt;
              &lt;div&gt;
                  &lt;label for="subject"&gt;Subject: &lt;/label&gt;
                  &lt;input type="text" name="lectureSearch" th:value="${lectureSearch}"&gt;
                  &lt;input type="submit" value="Search"&gt;
              &lt;/div&gt;
          &lt;/form&gt;
  
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;th&gt;Subject&lt;/th&gt;
                  &lt;th&gt;Short description&lt;/th&gt;
                  &lt;th&gt;Lecture Type&lt;/th&gt;
                  &lt;th&gt;Lecturer's Name&lt;/th&gt;
                  &lt;th&gt;Lecturer's position&lt;/th&gt;
                  &lt;th&gt;Published&lt;/th&gt;
              &lt;/tr&gt;
              &lt;tr th:each="lecture : ${lectureSearch}"&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.subject}" &gt;LECTURE.SUBJECT&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.shortDescription}" &gt;LECTURE.SHORTDESCRIPTION&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.lectureType}" &gt;LECTURE.LECTURETYPE&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.lecturer.name}" &gt;LECTURER.NAME&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.lecturer.lecturerPosition}" &gt;LECTURER.LECTURERPOSITION&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;span th:text="${lecture.published}" &gt;LECTURE.PUBLISHED&lt;/span&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
          &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Upon entering a subject in the search bar, no results can be seen, even though a lecture with the queried subject exists. Checking in the debugger, the variable <code>subject</code> is null even after the user types in the subject he wants to search for. 
  Any help would be appreciated.</p>