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; ...@@ -3,24 +3,30 @@ 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
Integer wynik = switch(operacja) { return "kalkulator.html";
case "+" -> liczba1 + liczba2; }
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2; @RequestMapping(path="/kalkulator", method=RequestMethod.POST)
case "/" -> liczba1 / liczba2; public String kalkulatorPost(Model model, Integer liczba1, Integer liczba2, String operacja) {
default -> 0; // Obsługa metody POST - zakładamy, że parametry zostały przekazane (a jeśli nie, to będzie wyjątek).
}; Integer wynik = switch(operacja) {
model.addAttribute("wynik", wynik); case "+" -> liczba1 + liczba2;
} case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
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