Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
b3b3b03c
Commit
b3b3b03c
authored
Oct 04, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kalkulator podstawowy (kopia)
parent
7dbc0332
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
1 deletions
+37
-1
KalkulatorPodstawowy.java
.../src/main/java/com/example/demo/KalkulatorPodstawowy.java
+36
-0
ParametryController.java
...g/src/main/java/com/example/demo/ParametryController.java
+1
-1
No files found.
PC29-Spring/src/main/java/com/example/demo/KalkulatorPodstawowy.java
0 → 100644
View file @
b3b3b03c
package
com
.
example
.
demo
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
@Controller
@RequestMapping
(
"/kalkulator-podstawowy"
)
public
class
KalkulatorPodstawowy
{
// W tej wersji obliczenie było realizowane w samej metodzie kontrolera.
// W większej skali byłoby to niewłaściwe podejście - mieszanie warstwy webowej (kontroler też do niej należy)
// z logiką aplikacji.
@GetMapping
public
String
kalkulatorGet
()
{
return
"kalkulator.html"
;
}
// Spring automatycznie przekaże parametry zapytania (dane z formularza)
// do tej metody - wystarczy zadeklarować parametry o tej samej nazwie.
@PostMapping
public
String
kalkulatorPost
(
Model
model
,
Long
liczba1
,
Long
liczba2
,
String
operacja
)
{
long
wynik
=
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
case
"%"
->
liczba1
%
liczba2
;
default
->
0
;
};
model
.
addAttribute
(
"wynik"
,
wynik
);
return
"kalkulator.html"
;
}
}
PC29-Spring/src/main/java/com/example/demo/ParametryController.java
View file @
b3b3b03c
...
@@ -22,7 +22,7 @@ public class ParametryController {
...
@@ -22,7 +22,7 @@ public class ParametryController {
// zadeklarować parametr o takiej samej nazwie w metodzie kontrolera.
// zadeklarować parametr o takiej samej nazwie w metodzie kontrolera.
// "Spring nam to przekaże".
// "Spring nam to przekaże".
// Podejście polegające na tym, że framework wywołuje "naszą metodę",
// Podejście polegające na tym, że framework wywołuje "naszą metodę",
// a gdy my czegoś
c
potrzebujemy, to dodajemy odp. parametr do tej metody,
// a gdy my czegoś potrzebujemy, to dodajemy odp. parametr do tej metody,
// nazywa się "inversion of control (IoC)".
// nazywa się "inversion of control (IoC)".
public
String
witaj
(
String
imie
)
{
public
String
witaj
(
String
imie
)
{
return
"Witaj "
+
imie
;
return
"Witaj "
+
imie
;
...
...
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