Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
f8ab0469
Commit
f8ab0469
authored
Nov 26, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wyświeltanie produktów za pomocą szablonów JSP
parent
cab6f0ba
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
26 deletions
+64
-26
pom.xml
PC30-SklepSpring/pom.xml
+0
-1
ProductController_v4.java
...alternatywne_wersje_bazy_danych/ProductController_v4.java
+53
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+11
-25
No files found.
PC30-SklepSpring/pom.xml
View file @
f8ab0469
...
@@ -65,7 +65,6 @@
...
@@ -65,7 +65,6 @@
<dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<groupId>
javax.servlet
</groupId>
<artifactId>
jstl
</artifactId>
<artifactId>
jstl
</artifactId>
<version>
1.2
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
...
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductController_v4.java
0 → 100644
View file @
f8ab0469
package
sklep
.
alternatywne_wersje_bazy_danych
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
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
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
sklep.model.Product
;
@Controller
@RequestMapping
(
path
=
"/alt4"
,
produces
=
"text/plain"
)
public
class
ProductController_v4
{
@Autowired
private
EntityManager
em
;
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
List
<
Product
>
products
=
query
.
getResultList
();
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
@GetMapping
(
"/{id}"
)
public
String
jedenProdukt
(
@PathVariable
int
id
,
Model
model
)
{
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
!=
null
)
{
model
.
addAttribute
(
"product"
,
product
);
return
"product"
;
}
else
{
model
.
addAttribute
(
"product_id"
,
id
);
return
"missing_product"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
query
.
setParameter
(
"name"
,
name
);
List
<
Product
>
products
=
query
.
getResultList
();
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
}
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
f8ab0469
...
@@ -2,6 +2,9 @@ package sklep.controller;
...
@@ -2,6 +2,9 @@ package sklep.controller;
import
java.util.List
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
...
@@ -9,10 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping;
...
@@ -9,10 +12,7 @@ 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
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
import
sklep.model.Product
;
import
sklep.model.Product
;
@Controller
@Controller
...
@@ -30,38 +30,24 @@ public class ProductController {
...
@@ -30,38 +30,24 @@ public class ProductController {
}
}
@GetMapping
(
"/{id}"
)
@GetMapping
(
"/{id}"
)
@ResponseBody
public
String
jedenProdukt
(
@PathVariable
int
id
,
Model
model
)
{
public
String
jedenProdukt
(
@PathVariable
int
id
)
{
StringBuilder
sb
=
new
StringBuilder
();
Product
product
=
em
.
find
(
Product
.
class
,
id
);
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
!=
null
)
{
if
(
product
!=
null
)
{
sb
.
append
(
"Znaleziony produkt:\n"
)
model
.
addAttribute
(
"product"
,
product
);
.
append
(
product
.
getProductName
())
return
"product"
;
.
append
(
" za cenę "
)
.
append
(
product
.
getPrice
())
.
append
(
'\n'
);
}
else
{
}
else
{
sb
.
append
(
"Nie ma produktu o numerze "
).
append
(
id
);
model
.
addAttribute
(
"product_id"
,
id
);
return
"missing_product"
;
}
}
return
sb
.
toString
();
}
}
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
@ResponseBody
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
public
String
wyszukajProdukty
(
@RequestParam
String
name
)
{
StringBuilder
sb
=
new
StringBuilder
();
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
query
.
setParameter
(
"name"
,
name
);
query
.
setParameter
(
"name"
,
name
);
List
<
Product
>
products
=
query
.
getResultList
();
List
<
Product
>
products
=
query
.
getResultList
();
for
(
Product
product
:
products
)
{
model
.
addAttribute
(
"products"
,
products
);
sb
.
append
(
"* produkt "
)
return
"products"
;
.
append
(
product
.
getProductName
())
.
append
(
" za cenę "
)
.
append
(
product
.
getPrice
())
.
append
(
'\n'
);
}
return
sb
.
toString
();
}
}
}
}
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