Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.AddressBook.spring;
- import java.util.List;
- import org.springframework.data.repository.CrudRepository;
- import org.springframework.transaction.annotation.Transactional;
- public interface ContactRepository extends CrudRepository<Contact, Long>{
- @Transactional
- List<Contact> findBySurname(String surname);
- @Transactional
- List<Contact> removeBySurname(String surname);
- }
- @RequestMapping(value = "/search", method = RequestMethod.GET)
- public String searchForm(Contact contact, @RequestParam (value = "surname", required = false) String surname, Model model) {
- return "search";
- }
- @RequestMapping(value = "/search", method = RequestMethod.POST)
- public String searchContact(Contact contact, @RequestParam (value = "surname", required = false) String surname, Model model) {
- model.addAttribute("search", contactRep.findBySurname(surname));
- return "search";
- }
- <!DOCTYPE HTML>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <title>search contact</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- </head>
- <body>
- <h1>Search contacts:</h1>
- <form th:object="${contacts}" th:action="@{/search}" method="get">
- <input type="text" name="search" id="search" th:value="${search}"/>
- <input type="submit" value="Search"/>
- <div th:if="${not #lists.isEmpty(contacts)}">
- <h2>Students List</h2>
- <table class="table table-striped">
- <tr>
- <th>Id</th>
- <th>Name</th>
- <th>Surname</th>
- <th>email</th>
- <th>phone number:</th>
- </tr>
- <tr th:each="contact: ${contacts}">
- <td th:text="${contact.id}">id</td>
- <td th:text="${contact.name}">name</td>
- <td th:text="${contact.surname}">surname</td>
- <td th:text="${contact.email}">email</td>
- <td th:text="${contact.phoneNumber}">phone number</td>
- </tr>
- </table>
- </div>
- <p><a href="/add">Add another contact</a> <a href="/list">Show list
- of contacts</a></p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement