Только осваиваю Spring и, вот такая задача - нужно осуществить поиск по разным параметрам. Причем параметры все не обязательные, т.е. могут присутствовать как несколько параметров сразу, так и только один из них. Опыта со Спрингом мало, поэтому, где ошибаюсь не знаю, но поиск не происходит. Есть html-форма:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}">Title</title>
<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">
<link rel="stylesheet" href="../static/css/styles.css" th:href="@{/css/styles.css}">
</head>
<body onload="checkFindFields();">
<div id="header_container">
<h1 class="header_content centered">Find Client</h1>
</div>
<div style="width: 300px; margin: 0 auto">
<form id="find_form" method="post" action="#" th:action="@{/clients/find}" th:object="${client}">
<p class="centered" style="width: 300px; margin: 20px">Find Client</p>
<table>
<tr>
<td style="width: 100px">ID: </td>
<td>
<label for="id"></label>
<input class="auth" style="margin: 5px" type="text" name="id" id="id" th:fileld="*{id}"/>
</td>
</tr>
<tr>
<td style="width: 100px">Name: </td>
<td>
<label for="name"></label>
<input class="auth" style="margin: 5px" type="text" name="name" id="name" th:field="*{firstName}"/>
</td>
</tr>
<tr>
<td style="width: 100px">Surname: </td>
<td>
<label for="surname"></label>
<input class="auth" style="margin: 5px" type="text" name="surname" id="surname" th:field="*{lastName}"/>
</td>
</tr>
<tr>
<td style="width: 100px">Email: </td>
<td>
<label for="email"></label>
<input class="auth" style="margin: 5px" type="text" name="email" id="email" th:field="*{email}"/>
</td>
</tr>
<tr>
<td style="width: 100px">Balance: </td>
<td>
<label for="balance"></label>
<input class="auth" style="margin: 5px" type="text" name="balance" id="balance" th:field="*{balance}"/>
</td>
</tr>
<tr>
<td style="width: 100px">Phones: </td>
<td>
<label for="phones"></label>
<input class="auth" style="margin: 5px" type="text" name="phones" id="phones"/>
</td>
</tr>
<tr>
<td style="width: 100px">Identity: </td>
<td>
<label for="identity"></label>
<input class="auth" style="margin: 5px" type="text" name="identity" id="identity"/>
</td>
</tr>
</table>
<table style="margin: 10px">
<tr>
<td><input style="width: 110px" type="submit" value="Find"/></td>
<td style="width: 80px"></td>
<td><input style="width: 110px" type="button" value="Cancel" onclick="openPage('/Client')"/></td>
</tr>
</table>
</form>
</div>
<script type="text/javascript" language="JavaScript" src="../static/js/main.js" th:src="@{/js/main.js}"></script>
</body>
</html>
Создал пользовательский интерфейс ClientRepositoryCustom:
public interface ClientRepositoryCustom {
List<Client> findAnyClient(Long id, String firstName, String lastName, String email, Double balance);}
Далее, класс, имплементирующий этот интерфейс:
@Repository
public class ClientRepositoryCustomImpl implements ClientRepositoryCustom {
@PersistenceContext
private EntityManager em;
public ClientRepositoryCustomImpl() {
}
@Override
public List<Client> findAnyClient(Long id, String firstName, String lastName, String email, Double balance) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Client> cq = cb.createQuery(Client.class);
Root<Client> clientRoot = cq.from(Client.class);
List<Predicate> predicates = new ArrayList<>();
if (id != null){
predicates.add(cb.equal(clientRoot.get("id"), id));
}
if (firstName != null){
predicates.add(cb.like(clientRoot.get("firstName"), firstName));
}
if (lastName != null){
predicates.add(cb.like(clientRoot.get("lastName"), lastName));
}
if (email != null){
predicates.add(cb.like(clientRoot.get("email"), email));
}
if (balance != null){
predicates.add(cb.equal(clientRoot.get("balance"), balance));
}
cq.where(predicates.toArray(new Predicate[0]));
return em.createQuery(cq).getResultList();
}}
И, фрагмент контроллера:
@Controller
public class ClientController {
@Autowired
private ClientService clientService;
@Autowired
private ClientRepositoryCustomImpl clientRepositoryCustom;
@RequestMapping(value = "/clients/find", method = RequestMethod.GET)
public String getFindFormCommand(Model model){
model.addAttribute("title", "Find Client");
model.addAttribute("client", new Client());
return "FindFormView";
}
@RequestMapping(value = "/clients/find", method = RequestMethod.POST)
public String findClient(@ModelAttribute Client client, Model model,
@RequestParam(value = "id") Long id,
@RequestParam(value = "firstName") String firstName,
@RequestParam(value = "lastName") String lastName,
@RequestParam(value = "email") String email,
@RequestParam(value = "balance") Double balance){
model.addAttribute("title", "Find Client");
List<Client> clientList = clientRepositoryCustom.findAnyClient(id, firstName, lastName, email, balance);
model.addAttribute("clientList", clientList);
return "ViewCommand";
}}
Укажите, что делаю не так? Где ошибки, - почему не работает поиск?