Commit aecddd39 by Patryk Czarnik

Wyszukiwarka - pierwsza wersja

parent 14fa538b
package sklep.controller; package sklep.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
...@@ -20,7 +22,7 @@ public class ProductController { ...@@ -20,7 +22,7 @@ public class ProductController {
@GetMapping @GetMapping
public String odczytajProdukty(Model model) { public String odczytajProdukty(Model model) {
List<Product> products = productRepository.findAll(); List<Product> products = productRepository.findAll(Sort.by("id"));
model.addAttribute("products", products); model.addAttribute("products", products);
return "products"; return "products";
} }
...@@ -36,4 +38,12 @@ public class ProductController { ...@@ -36,4 +38,12 @@ public class ProductController {
return "missing_product"; return "missing_product";
} }
} }
@GetMapping("/szukaj")
public String wyszukiwarka(Model model,
@RequestParam("name") String productName) {
List<Product> products = productRepository.findByProductName(productName);
model.addAttribute("products", products);
return "wyszukiwarka1";
}
} }
...@@ -3,6 +3,9 @@ package sklep.repository; ...@@ -3,6 +3,9 @@ package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Product; import sklep.model.Product;
import java.util.List;
// Rozszerzamy interfejs JpaRepository określając typ rekordu oraz typ klucza // Rozszerzamy interfejs JpaRepository określając typ rekordu oraz typ klucza
public interface ProductRepository extends JpaRepository<Product, Integer> { public interface ProductRepository extends JpaRepository<Product, Integer> {
List<Product> findByProductName(String productName);
} }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Katalog towarów</title>
<link rel="stylesheet" type="text/css" href="/styl.css"/>
</head>
<body>
<div><a href="/">Powrót do spisu treści</a></div>
<h1>Wyszukiwarka produktów</h1>
<form method="get" class="wyszukiwarka">
<table>
<tr><td><label for="name">Podaj nazwę:</label></td>
<td><input type="text" id="name" name="name" value="${param.name}"></td></tr>
<tr><td><button>Szukaj</button></td></tr>
</table>
</form>
<c:forEach var="product" items="${products}">
<div class="product">
<img class="photo" src="/products/${product.id}/photo" alt=""/>
<p>Towar <a href="/products/${product.id}" class="product-name">${product.productName}</a></p>
<p>Cena: <span class="product-price">${product.price}</span></p>
<p class="product-description">${product.description}</p>
</div>
</c:forEach>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment