Mình đang tập viết 1 website đơn giản, mình làm theo clip hướng dẫn thì không phát sinh lỗi nào và chạy được nhưng khi thử chức năng tìm kiếm thì nó lại không hoạt động.
Mình sử dụng SpringBoot, Thymeleaf.
Entity
package com.example.demo.models;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Table(name = "book")
@Entity
@Data
public class Book implements Serializable{
@Id
private Long id;
@Column
private String name;
@Column
private String image;
}
Controller
package com.example.demo.controllers;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.models.Book;
import com.example.demo.services.BookService;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/")
public String add(ModelMap model) {
model.addAttribute("book", new Book());
return "add";
}
@PostMapping("/save")
public String save(ModelMap model, Book book) {
bookService.save(book);
model.addAttribute(book);
return list(model);
}
@GetMapping("/list")
public String list(ModelMap model) {
List<Book> list = (List<Book>) bookService.findAll();
model.addAttribute("books", list);
return "list";
}
@GetMapping("/edit/{id}")
public String edit(ModelMap model, @PathVariable(name= "id")Long id) {
Optional<Book> opt= bookService.findById(id);
if(opt.isPresent()) {
model.addAttribute("book",opt.get());
}
return "add";
}
@GetMapping("/delete/{id}")
public String delete(ModelMap model,@PathVariable(name = "id")Long id) {
bookService.deleteById(id);
return list(model);
}
@GetMapping("/find")
public String find(ModelMap model, @RequestParam(defaultValue = "") String name) {
List<Book> list = bookService.findByNameLikeOrderByName(name);
model.addAttribute("books",list);
return list(model);
}
}
Repository
package com.example.demo.repostories;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.models.Book;
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
List<Book> findByNameLikeOrderByName(String name);
}
Service
package com.example.demo.services;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.models.Book;
@Service
public interface BookService {
void deleteAll();
void deleteAll(Iterable<? extends Book> entities);
void delete(Book entity);
void deleteById(Long id);
long count();
Iterable<Book> findAllById(Iterable<Long> ids);
Iterable<Book> findAll();
boolean existsById(Long id);
Optional<Book> findById(Long id);
<S extends Book> Iterable<S> saveAll(Iterable<S> entities);
<S extends Book> S save(S entity);
List<Book> findByNameLikeOrderByName(String name);
}
package com.example.demo.services;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.models.Book;
import com.example.demo.repostories.BookRepository;
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookRepository bookRepository;
@Override
public <S extends Book> S save(S entity) {
return bookRepository.save(entity);
}
@Override
public <S extends Book> Iterable<S> saveAll(Iterable<S> entities) {
return bookRepository.saveAll(entities);
}
@Override
public Optional<Book> findById(Long id) {
return bookRepository.findById(id);
}
@Override
public boolean existsById(Long id) {
return bookRepository.existsById(id);
}
@Override
public Iterable<Book> findAll() {
return bookRepository.findAll();
}
public List<Book> findByNameLikeOrderByName(String name) {
return bookRepository.findByNameLikeOrderByName( "%" + name + "%");
}
@Override
public Iterable<Book> findAllById(Iterable<Long> ids) {
return bookRepository.findAllById(ids);
}
@Override
public long count() {
return bookRepository.count();
}
@Override
public void deleteById(Long id) {
bookRepository.deleteById(id);
}
@Override
public void delete(Book entity) {
bookRepository.delete(entity);
}
@Override
public void deleteAll(Iterable<? extends Book> entities) {
bookRepository.deleteAll(entities);
}
@Override
public void deleteAll() {
bookRepository.deleteAll();
}
}
HTML
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>List Book</title>
</head>
<body>
<h1>List all book</h1>
<form th:action="@{/find}">
name:
<input type="text" name="name" placeholder="Name"/>
<input type="submit" value="Find"/>
</form>
<hr />
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Image</td>
<td> </td>
</tr>
<tr th:each="item:${books}">
<td th:text="${item.id}"></td>
<td th:text="${item.name}"></td>
<td th:text="${item.image}"></td>
<td>
<a th:href="@{'/edit/' + ${item.id}}">Edit</a>
<a th:href="@{'/delete/' + ${item.id}}">Delete</a>
</td>
</tr>
</table>
</body>
</html>
Khi mình tìm kiếm thì nó lại không hoạt động và thông báo lỗi nên không biết sửa sao