Commit ed78cacf by Patryk Czarnik

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

parent fb592949
......@@ -3,24 +3,30 @@ package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Kalkulator {
@RequestMapping("/kalkulator")
public String dzialaj(Model model, Integer liczba1, Integer liczba2, String operacja) {
if(liczba1 != null && liczba2 != null) {
Integer wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
model.addAttribute("wynik", wynik);
}
@RequestMapping(path="/kalkulator", method=RequestMethod.GET)
public String kalkulatorGet() {
// 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) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
model.addAttribute("wynik", wynik);
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