Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
5c9b22e0
Commit
5c9b22e0
authored
Apr 24, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Formularz edycji danych
parent
36eb42ab
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
232 additions
and
1 deletions
+232
-1
AddToBasket.java
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
+1
-1
EditProduct.java
PC25-SklepWeb/src/main/java/sklep/web/EditProduct.java
+81
-0
product_form.jsp
PC25-SklepWeb/src/main/webapp/product_form.jsp
+48
-0
products9.jsp
PC25-SklepWeb/src/main/webapp/products9.jsp
+69
-0
styl.css
PC25-SklepWeb/src/main/webapp/styl.css
+33
-0
No files found.
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
View file @
5c9b22e0
...
...
@@ -32,7 +32,7 @@ public class AddToBasket extends HttpServlet {
// ignorujemy błędy
}
// Przekierowanie - każemy przeglądarce wejść pod ten adres.
response
.
sendRedirect
(
"products
8
.jsp"
);
response
.
sendRedirect
(
"products
9
.jsp"
);
}
}
PC25-SklepWeb/src/main/java/sklep/web/EditProduct.java
0 → 100644
View file @
5c9b22e0
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_product"
)
public
class
EditProduct
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
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
);
}
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.
response
.
sendRedirect
(
"products9.jsp"
);
}
catch
(
DBException
e
)
{
e
.
printStackTrace
();
}
}
}
PC25-SklepWeb/src/main/webapp/product_form.jsp
0 → 100644
View file @
5c9b22e0
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/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
name=
"productId"
placeholder=
"brak"
type=
"number"
readonly=
"readonly"
value=
"${product.productId}"
/></td>
</tr>
<tr>
<td><label
for=
"productName"
>
Nazwa towaru:
</label></td>
<td><input
name=
"productName"
placeholder=
"nazwa..."
type=
"text"
value=
"${product.productName}"
/>
</td>
</tr>
<tr>
<td><label
for=
"price"
>
Cena:
</label></td>
<td><input
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
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
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>
PC25-SklepWeb/src/main/webapp/products9.jsp
0 → 100644
View file @
5c9b22e0
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/core"
%
>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Lista produktów 9
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Lista produktów - wersja 9
</h1>
<jsp:useBean
id=
"bean"
class=
"sklep.web.ProductBean"
/>
<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></li>
</c:forEach>
</ul>
<p
class=
"total"
>
Do zapłaty: ${basket.totalValue}
</p>
</div>
<form
id=
"wyszukiwarka"
method=
"get"
>
<h2>
Filtr cen
</h2>
<table
class=
"formularz"
>
<tr><td><label
for=
"min_price"
>
Cena minimalna:
</label></td>
<td><input
type=
"number"
name=
"min_price"
value=
"${param.min_price}"
></td></tr>
<tr><td><label
for=
"max_price"
>
Cena maksymalna:
</label></td>
<td><input
type=
"number"
name=
"max_price"
value=
"${param.max_price}"
></td></tr>
<tr><td><button>
Szukaj
</button></td></tr>
</table>
</form>
<div>
Produkty o cenach
<c:if
test=
"${not empty(param.min_price)}"
>
od ${param.min_price}
</c:if>
<c:if
test=
"${not empty(param.max_price)}"
>
do ${param.max_price}
</c:if>
</div>
<jsp:setProperty
name=
"bean"
property=
"minPrice"
param=
"min_price"
/>
<jsp:setProperty
name=
"bean"
property=
"maxPrice"
param=
"max_price"
/>
<c:forEach
var=
"p"
items=
"${bean.filteredProducts}"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"photo?productId=${p.productId}"
alt=
""
/>
<h3>
${p.productName}
</h3>
<div
class=
"price"
>
Cena: ${p.price}
</div>
<div
class=
"price"
>
VAT ${p.vat * 100}%
</div>
<c:if
test=
"${not empty p.description}"
>
<p>
${p.description}
</p>
</c:if>
<div
class=
"action"
><a
href=
"add_to_basket?productId=${p.productId}"
>
dodaj do koszyka
</a></div>
<div
class=
"action"
><a
href=
"edit_product?productId=${p.productId}"
>
edytuj
</a></div>
<div
class=
"action"
><a
href=
"upload_photo.jsp?productId=${p.productId}"
>
zmień zdjęcie
</a></div>
</div>
</c:forEach>
<div
class=
"action"
><a
href=
"edit_product"
>
Dodaj nowy produkt
</a></div>
</body>
</html>
PC25-SklepWeb/src/main/webapp/styl.css
View file @
5c9b22e0
...
...
@@ -62,3 +62,36 @@ h2, h3, h4 {
font-style
:
italic
;
}
div
.action
{
font-size
:
smaller
;
font-family
:
'Arial'
,
sans-serif
;
font-weight
:
bold
;
background-color
:
#DDDDDD
;
border
:
2px
#444466
outset
;
padding
:
6px
;
margin
:
4px
auto
4px
4px
;
max-width
:
200px
;
}
.action
:hover
{
background-color
:
#EEEEEE
;
border
:
2px
#4455CC
outset
;
}
.action
:active
{
background-color
:
#EEEEEE
;
border
:
2px
#CC4455
inset
;
}
.action
a
{
display
:
inline-block
;
color
:
inherit
;
text-decoration
:
none
;
width
:
100%
;
}
.action
a
:hover
{
color
:
#0000CC
;
}
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