Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
alx_java2b_20250412
Commits
b61118ef
Commit
b61118ef
authored
Apr 27, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SklepWeb - edycja danych produktu
parent
09ca45cd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
152 additions
and
6 deletions
+152
-6
EditProduct.java
PC23-SklepWeb/src/main/java/sklep/web/EditProduct.java
+80
-0
web.xml
PC23-SklepWeb/src/main/webapp/WEB-INF/web.xml
+7
-0
product_form.jsp
PC23-SklepWeb/src/main/webapp/product_form.jsp
+48
-0
products7.jsp
PC23-SklepWeb/src/main/webapp/products7.jsp
+2
-4
products8.jsp
PC23-SklepWeb/src/main/webapp/products8.jsp
+15
-2
No files found.
PC23-SklepWeb/src/main/java/sklep/web/EditProduct.java
0 → 100644
View file @
b61118ef
package
sklep
.
web
;
import
java.io.IOException
;
import
java.math.BigDecimal
;
import
jakarta.servlet.RequestDispatcher
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Product
;
@WebServlet
(
"/edit"
)
public
class
EditProduct
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
parametrId
=
request
.
getParameter
(
"productId"
);
if
(
parametrId
!=
null
)
{
int
productId
=
Integer
.
parseInt
(
parametrId
);
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
// Gdy do obiektu request dodamy atrybut, to stanie się on dostępny dla kolejnych komponentów
// naszej aplikacji, które będą obsługiwać to zapytanie.
// W tym przypadku skrypt JSP może odwoływać się do obiektu product.
// Obiekt request jest też nośnikiem danych, podobnie jak sesja i servletContext.
// To działa jak Model w Spring MVC.
// Tylko jeśli znajdę produkt, tylko wtedy dodaję go do requestu i JSP wyświetli jego dane.
// Jeśli parametru productId nie było lub produktu nie znaleziono, to wyświetli się pusty formularz.
request
.
setAttribute
(
"product"
,
product
);
}
catch
(
DBException
|
RecordNotFound
e
)
{
e
.
printStackTrace
();
}
}
// Forward to "wewnętrzne przekierowanie" obsługi zapytania do innego komponentu aplikacji.
// Tutaj "wyświetlamy" formularz edycji produktu.
RequestDispatcher
dispatcher
=
request
.
getRequestDispatcher
(
"product_form.jsp"
);
if
(
dispatcher
!=
null
)
dispatcher
.
forward
(
request
,
response
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// W tej wersji nie obsługujemy błędów - w razie błędu wyświetli się strona z wyjątkiem
// W przypadku braku ID zostanie utworzony nowy produkt, a w przypadku podania ID (gdy to była edycja istniejącego) - zostanie zastąpiony.
request
.
setCharacterEncoding
(
"UTF-8"
);
String
parametrId
=
request
.
getParameter
(
"productId"
);
Integer
productId
=
(
parametrId
==
null
||
parametrId
.
isEmpty
())
?
null
:
Integer
.
valueOf
(
parametrId
);
String
parametrPrice
=
request
.
getParameter
(
"price"
);
BigDecimal
price
=
new
BigDecimal
(
parametrPrice
);
String
parametrVat
=
request
.
getParameter
(
"vat"
);
BigDecimal
vat
=
(
parametrVat
==
null
||
parametrVat
.
isEmpty
())
?
null
:
new
BigDecimal
(
parametrVat
);
String
name
=
request
.
getParameter
(
"productName"
);
String
description
=
request
.
getParameter
(
"description"
);
Product
product
=
new
Product
(
productId
,
name
,
price
,
vat
,
description
);
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
// Gdy udało się zapisać, to przejdziemy z powrotem do listy.
// To jest przekierowanie przeglądarki do inny adres.
response
.
sendRedirect
(
"products9.jsp"
);
}
catch
(
DBException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-SklepWeb/src/main/webapp/WEB-INF/web.xml
0 → 100644
View file @
b61118ef
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns=
"https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version=
"6.0"
>
</web-app>
\ No newline at end of file
PC23-SklepWeb/src/main/webapp/product_form.jsp
0 → 100644
View file @
b61118ef
<
%@
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>
Edycja danych produktu
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Edycja produktu
</h1>
<form
id=
"product-form"
method=
"post"
>
<table
class=
"form"
>
<tr>
<td><label
for=
"productId"
>
Numer:
</label></td>
<td><input
id=
"productId"
name=
"productId"
placeholder=
"brak"
type=
"number"
readonly=
"readonly"
value=
"${product.productId}"
/></td>
</tr>
<tr>
<td><label
for=
"productName"
>
Nazwa towaru:
</label></td>
<td><input
id=
"productName"
name=
"productName"
placeholder=
"nazwa..."
type=
"text"
value=
"${product.productName}"
/>
</td>
</tr>
<tr>
<td><label
for=
"price"
>
Cena:
</label></td>
<td><input
id=
"price"
name=
"price"
placeholder=
"12.90"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.price}"
/>
</td>
</tr>
<tr>
<td><label
for=
"vat"
>
Stawka VAT:
</label></td>
<td><input
id=
"vat"
name=
"vat"
placeholder=
"0.23"
title=
"tu wpisz vat"
type=
"number"
step=
"0.01"
value=
"${product.vat}"
/>
</td>
</tr>
<tr>
<td><label
for=
"description"
>
Opis:
</label></td>
<td><textarea
id=
"description"
name=
"description"
rows=
"10"
cols=
"120"
>
${product.description}
</textarea></td>
</tr>
<tr>
<td><button>
Zapisz
</button></td>
</tr>
</table>
</form>
<div
class=
"action"
><a
href=
"products9.jsp"
>
powrót do listy produktów
</a></div>
<div
class=
"action"
><a
href=
"index.html"
>
powrót do spisu treści
</a></div>
</body>
</html>
PC23-SklepWeb/src/main/webapp/products7.jsp
View file @
b61118ef
...
...
@@ -4,11 +4,11 @@
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Lista produktów
8
</title>
<title>
Lista produktów
7
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Lista produktów - wersja
8
</h1>
<h1>
Lista produktów - wersja
7
</h1>
<div
class=
"koszyk"
>
<h4>
Koszyk
</h4>
...
...
@@ -46,9 +46,7 @@
<p
class=
"description"
>
${product.description}
</p>
</c:if>
<div
class=
"action"
><a
href=
"add_to_basket?productId=${product.productId}"
>
dodaj do koszyka
</a></div>
<div
class=
"action"
><a
href=
"edit?productId=${product.productId}"
>
edytuj
</a></div>
</div>
</c:forEach>
<div
class=
"action"
><a
href=
"edit"
>
Dodaj nowy produkt
</a></div>
</body>
</html>
PC23-SklepWeb/src/main/webapp/products8.jsp
View file @
b61118ef
...
...
@@ -4,11 +4,23 @@
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Lista produktów
7
</title>
<title>
Lista produktów
8
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Lista produktów - wersja 7
</h1>
<h1>
Lista produktów - wersja 8
</h1>
<div
class=
"koszyk"
>
<h4>
Koszyk
</h4>
<ul>
<
%
--
Zauwa
ż
my
,
ż
e
dla
obiektu
koszyk
nie
wykonujemy
ju
ż
useBean
.
Po
prostu
zak
ł
adamy
,
ż
e
jest
obecny
(
w
sesji
).
Gdyby
go
nie
by
ł
o
,
to
p
ę
tla
si
ę
nie
wykona
.
--
%
>
<c:forEach
var=
"elm"
items=
"${basket.elements}"
>
<li>
${elm.productName} (${elm.quantity}) za
<b>
${elm.value}
</b>
<a
href=
"remove_from_basket?productId=${elm.productId}"
>
(–)
</a></li>
</c:forEach>
</ul>
<p
class=
"total"
>
Do zapłaty: ${basket.totalValue}
</p>
</div>
<form
id=
"wyszukiwarka"
method=
"get"
>
<h2>
Filtr cen
</h2>
...
...
@@ -33,6 +45,7 @@
<c:if
test=
"${not empty(product.description)}"
>
<p
class=
"description"
>
${product.description}
</p>
</c:if>
<div
class=
"action"
><a
href=
"add_to_basket?productId=${product.productId}"
>
dodaj do koszyka
</a></div>
<div
class=
"action"
><a
href=
"edit?productId=${product.productId}"
>
edytuj
</a></div>
</div>
</c:forEach>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment