[ed20c2c] | 1 | package com.example.baza.web;
|
---|
| 2 |
|
---|
| 3 | import com.example.baza.model.Chovek2;
|
---|
| 4 | import com.example.baza.model.exception.InvalidUserCredentialsException;
|
---|
| 5 | import com.example.baza.service.ChovekService;
|
---|
| 6 | import org.springframework.stereotype.Controller;
|
---|
| 7 | import org.springframework.ui.Model;
|
---|
| 8 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 9 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 10 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 11 |
|
---|
| 12 | import javax.servlet.http.HttpServletRequest;
|
---|
| 13 |
|
---|
| 14 | @Controller
|
---|
| 15 | @RequestMapping("/login")
|
---|
| 16 | public class LoginController {
|
---|
| 17 |
|
---|
| 18 | private final ChovekService chovekService;
|
---|
| 19 |
|
---|
| 20 | public LoginController(ChovekService chovekService) {
|
---|
| 21 | this.chovekService = chovekService;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | @GetMapping
|
---|
| 25 | public String getLoginPage(){
|
---|
| 26 | return "login";
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | @PostMapping
|
---|
| 30 | public String login(HttpServletRequest request, Model model){
|
---|
| 31 | Chovek2 chovek = null;
|
---|
| 32 | try{
|
---|
| 33 | chovek = this.chovekService.login(request.getParameter("username"),
|
---|
| 34 | request.getParameter("password"));
|
---|
| 35 | request.getSession().setAttribute("user",chovek);
|
---|
| 36 | return "redirect:/home";
|
---|
| 37 | }
|
---|
| 38 | catch (InvalidUserCredentialsException exception)
|
---|
| 39 | {
|
---|
| 40 | model.addAttribute("hasError", true);
|
---|
| 41 | model.addAttribute("error", exception.getMessage());
|
---|
| 42 | return "login";
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | }
|
---|