Commit ed78cacf by Patryk Czarnik

Rozdzielenie obsługi GET i POST (parametr method w adnotacji)

parent fb592949
...@@ -3,13 +3,20 @@ package com.example.demo; ...@@ -3,13 +3,20 @@ package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller @Controller
public class Kalkulator { public class Kalkulator {
@RequestMapping("/kalkulator") @RequestMapping(path="/kalkulator", method=RequestMethod.GET)
public String dzialaj(Model model, Integer liczba1, Integer liczba2, String operacja) { public String kalkulatorGet() {
if(liczba1 != null && liczba2 != null) { // Obsługa metody GET - tylko wyświetlenie pustego formularza
return "kalkulator.html";
}
@RequestMapping(path="/kalkulator", method=RequestMethod.POST)
public String kalkulatorPost(Model model, Integer liczba1, Integer liczba2, String operacja) {
// Obsługa metody POST - zakładamy, że parametry zostały przekazane (a jeśli nie, to będzie wyjątek).
Integer wynik = switch(operacja) { Integer wynik = switch(operacja) {
case "+" -> liczba1 + liczba2; case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2; case "-" -> liczba1 - liczba2;
...@@ -18,7 +25,6 @@ public class Kalkulator { ...@@ -18,7 +25,6 @@ public class Kalkulator {
default -> 0; default -> 0;
}; };
model.addAttribute("wynik", wynik); model.addAttribute("wynik", wynik);
}
return "kalkulator.html"; return "kalkulator.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