-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserController.java
116 lines (100 loc) · 3.23 KB
/
UserController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.food.foodweb.controller;
import com.food.foodweb.model.Food;
import com.food.foodweb.model.User;
import com.food.foodweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class UserController {
public UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@GetMapping("/login")
public String toLogin() {
return "login";
}
@GetMapping("/usersy")
public String toUsersy() {
return "usersy";
}
@PostMapping("/login")
public String login(User user, HttpSession session, Model model) {
User ret = userService.findUser(user);
if (ret != null) {
session.setAttribute("user", ret);
model.addAttribute("msg", "Login successful! Start food surfing!");
return "usersy";
} else {
model.addAttribute("msg", "Username or password is incorrect");
return "login";
}
}
@GetMapping("/exit")
public String exit(HttpSession session) {
session.removeAttribute("user");
return "redirect:login";
}
@GetMapping("/register")
public String toRegister() {
return "register";
}
@PostMapping("/register")
public String toRegister(User user, Model model) {
boolean ret = userService.addUser(user);
if (ret) {
model.addAttribute("msg", "Registration successful! Please log in with your account!");
return "sy";
} else {
model.addAttribute("msg", "registration failed!");
return "sy";
}
}
@GetMapping("/edituser")
public String toEdit(String username, Model model) {
User user = userService.findUserByName(username);
model.addAttribute("u", user);
return "usercenter";
}
@PostMapping("/edituser")
public String doEdit(User user, Model model) {
boolean ret = userService.updateuser(user);
System.out.println(ret);
if (ret) {
return "redirect:usersy";
} else {
return "usercenter";
}
}
@GetMapping("/UserManageSy")
public String CommentManageSy(Model model) {
List<User> users = userService.findAllUser();
model.addAttribute("users", users);
return "usermanagesy";
}
@GetMapping("/userDel")
public String userDel(String name) {
boolean ret = userService.delUserByName(name);
return "redirect:UserManageSy";
}
@GetMapping("/userEditByManager")
public String userEdit(String username, Model model) {
User user = userService.findUserByName(username);
model.addAttribute("userE", user);
return "useredit";
}
@PostMapping("/userEditByManager")
public String doUserEdit(User user) {
boolean ret = userService.updateuser(user);
if (ret) {
return "redirect:UserManageSy";
} else {
return "useredit";
}
}
}