Todoリストで検索機能を作っているのですが、タイトルにもある通り検索結果が0の場合の処理をしたいのですがうまくいきません。
java
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 7 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> 8 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> 9 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 10 11</head> 12<body> 13<div th:replace="/common.html::header"></div> 14<hr/> 15</br> 16</br> 17</br> 18 19 20<div class="container"> 21<form method="POST" th:action="@{/search}" > 22 23 24 <input class="form-control" type="text" name="title" required/> 25 </br> 26 27 <div class="text-center"> 28 <input class="btn btn-outline-secondary" type="submit" value="検索" /> 29 </div> 30 31 32</form> 33 34 <table class="table table-bordered"> 35 <tbody> 36 <tr th:each="todo, list :${todos}" th:object="${todo}"> 37 38 <p th:text="'Todoが' + ${list.count} + '件見つかりました'"></p> 39 40 <!--- 41 <div th:if="#lists.size(todos)"> 42 <P>対象のToDoは見つかりません</P> 43 </div> 44 45 <div th:if="${list.count == 0}"> 46 <P>対象のToDoは見つかりません</P> 47 </div> 48 49 ---> 50 51 52 <div th:if="${todo.done == false}"> 53 <td th:text="*{title}"></td> 54 <td th:text="*{day}"></td> 55 <td th:text="${#dates.format(todo.createDay, '(yyyy-MM-dd )')}">(1970-01-01 )</td> 56 </div> 57 58 <td><a class="btn btn-outline-dark" th:href="@{/{id}/edit(id=*{id})}">編集</a></td> 59 60 61 <td> 62 <form th:unless="${todo.done}" method="post" th:action="@{/done}" th:object="${todo}"> 63 <input type="hidden" name="id" th:value="*{id}" /> 64 <input class="btn btn-outline-danger" type="submit" value="未完了" /> 65 </form> 66 </td> 67 </tr> 68 </tbody> 69 </table> 70 71</div> 72</body> 73</html>
java
1@RequestMapping(value ="/search", method = RequestMethod.POST) 2 public String search(@RequestParam("title") String title, Model model) { 3 List<Todo> todos = service.searchTodo(title); 4 model.addAttribute("todos", todos); 5 return "search"; 6 }
java
1@Repository 2public interface TodoRepository extends JpaRepository<Todo, Long> { 3 List<Todo> findByTitleContainingOrderByIdAsc(String title); 4}
java
1@Service 2@Transactional 3public class TodoService { 4 5 @Autowired 6 private TodoRepository repository; 7 8 public List<Todo> findAll() { 9 return repository.findAll(new Sort(Sort.Direction.DESC, "id")); 10 } 11 12 public Todo getOne(Long id) { 13 return repository.getOne(id); 14 } 15 16 public Todo save(Todo todo) { 17 return repository.save(todo); 18 } 19 20 public List<Todo> searchTodo(String title){ 21 return repository.findByTitleContainingOrderByIdAsc(title); 22 } 23 24} 25
java
1@Entity 2@Table(name = "todoitems") 3public class Todo { 4 @Id 5 @GeneratedValue(strategy = GenerationType.IDENTITY) 6 7 private long id; 8 9 @NotNull 10 @Size(min = 1, max = 30) 11 private String title; 12 13 @NotNull 14 @Size(min = 1, max = 30) 15 private String day; 16 17 18 private Boolean done ; 19 20 @Temporal(TemporalType.DATE) 21 @Column(updatable = false) 22 private Date createDay; 23 24 @PrePersist 25 public void prePersist() { 26 this.createDay = new Date(); 27 } 28 29 public Date getCreateDay() { 30 return createDay; 31 } 32 33 34 public Long getId() { 35 return id; 36 } 37 38 public void setId(Long id) { 39 this.id = id; 40 } 41 42 public String getTitle() { 43 return title; 44 } 45 46 public void setTitle(String title) { 47 this.title = title; 48 } 49 50 public String getDay() { 51 return day; 52 } 53 54 public void setDay(String day) { 55 this.day = day; 56 } 57 58 59 public Boolean getDone() { 60 return done; 61 } 62 63 public void setDone(Boolean done) { 64 this.done = done; 65 } 66 67 68 69 70 71} 72 73
検索結果が0の場合の処理をするにはどのような書き方をするべきなのでしょうか?
何卒よろしくお願いします!
回答2件
あなたの回答
tips
プレビュー