Commit d130c3fb by Patryk Czarnik

Odczyt produktów - pierwsza wersja z JpaRepository

parent 4c2ecab5
<?xml version="1.0" encoding="UTF-8"?> <!--<?xml version="1.0" encoding="UTF-8"?>-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version> <version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>pl.alx.kjava</groupId> <groupId>pl.alx.kjava</groupId>
<artifactId>PC30-SklepSpring</artifactId> <artifactId>PC30-SklepSpring</artifactId>
...@@ -55,7 +56,7 @@ ...@@ -55,7 +56,7 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- dopisane zależności, aby działało JSP --> <!-- dopisane zależności, aby działało JSP -->
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
...@@ -65,6 +66,10 @@ ...@@ -65,6 +66,10 @@
<groupId>jakarta.servlet.jsp.jstl</groupId> <groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package sklep.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import sklep.model.Product;
import sklep.repository.ProductRepository;
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping("/products")
public String readAll(Model model) {
List<Product> products = productRepository.findAll();
model.addAttribute("products", products);
return "products.jsp";
}
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Product;
public interface ProductRepository extends JpaRepository<Product, Integer> {
}
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Aplikacja Sklep Spring</title> <title>Aplikacja Sklep Spring</title>
<link rel="stylesheet" type="text/css" href="../../styl.css"> <link rel="stylesheet" type="text/css" href="/styl.css">
</head> </head>
<body> <body>
<h1>Spis treści</h1> <h1>Spis treści</h1>
......
<%@ 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>
<c:if test="${not empty basket and not empty basket.elements}">
<div class="basket">
<h4>Koszyk</h4>
<ul>
<c:forEach var="p" items="${basket.elements}">
<li>${p.productName}: ${p.quantity} × ${p.price} = <b>${p.value}</b></li>
</c:forEach>
</ul>
<p>Wartość koszyka: ${basket.totalValue}</p>
</div>
</c:if>
<h1>Wszystkie produkty</h1>
<c:forEach var="product" items="${products}">
<div class="product">
<img class="photo" src="/products/${product.productId}/photo" alt=""/>
<p>Towar <a href="/products/${product.productId}" 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 class="action"><a href="/products/${product.productId}/edit">Edytuj</a></div>
<div class="action"><a href="/products/${product.productId}/add-to-basket">Dodaj do koszyka</a></div>
</div>
</c:forEach>
<div><a href="/products/new">Dodaj nowy produkt</a></div>
<div><a href="/">Wróć na stronę główną</a></div>
</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