source: src/main/java/com/example/baza/web/LoginController.java

Last change on this file was ed20c2c, checked in by HumaSejdini <humasejdini12@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[ed20c2c]1package com.example.baza.web;
2
3import com.example.baza.model.Chovek2;
4import com.example.baza.model.exception.InvalidUserCredentialsException;
5import com.example.baza.service.ChovekService;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.GetMapping;
9import org.springframework.web.bind.annotation.PostMapping;
10import org.springframework.web.bind.annotation.RequestMapping;
11
12import javax.servlet.http.HttpServletRequest;
13
14@Controller
15@RequestMapping("/login")
16public 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}
Note: See TracBrowser for help on using the repository browser.