Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. AuthorizationController
  2.  
  3.  
  4. package adil.java.schoolmaven.controller;
  5.  
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.servlet.ModelAndView;
  11.  
  12. @Controller
  13. public class AuthorizationController{
  14.  
  15. // If user will be successfully authenticated he/she will be taken to the login secure page.
  16. @RequestMapping(value="/admin", method = RequestMethod.GET)
  17. public ModelAndView adminPage() {
  18.  
  19. ModelAndView m = new ModelAndView();
  20. m.addObject("title", "Вы успешно вошли");
  21. m.addObject("message", "Основная");
  22. m.setViewName("admin");
  23.  
  24. return m;
  25. }
  26.  
  27. // Spring security will see this message.
  28. @RequestMapping(value = "/login", method = RequestMethod.POST)
  29. public ModelAndView login(@RequestParam(value = "error", required = false) String error,
  30. @RequestParam(value = "logout", required = false) String logout) {
  31.  
  32. ModelAndView m = new ModelAndView();
  33. if (error != null) {
  34. m.addObject("error", "Неверный логин и пароль");
  35. }
  36.  
  37. if (logout != null) {
  38. m.addObject("msg", "Вы успешно вышли");
  39. }
  40.  
  41. m.setViewName("login");
  42. redirAttrbs.addFlashAttribute("ключ", "параметр");
  43. return new ModelAndView("redirect:/allStudents");
  44. }
  45. }
  46.  
  47. StudentController
  48. package adil.java.schoolmaven.controller;
  49.  
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.util.List;
  53. import javax.servlet.ServletContext;
  54. import adil.java.schoolmaven.entity.Student;
  55. import adil.java.schoolmaven.service.StudentService;
  56. import java.nio.file.FileSystemException;
  57. import javax.servlet.http.HttpServletRequest;
  58. import org.springframework.beans.factory.annotation.Autowired;
  59. import org.springframework.lang.NonNull;
  60. import org.springframework.stereotype.Controller;
  61. import org.springframework.web.bind.annotation.GetMapping;
  62. import org.springframework.web.bind.annotation.PathVariable;
  63. import org.springframework.web.bind.annotation.PostMapping;
  64. import org.springframework.web.bind.annotation.RequestMapping;
  65. import org.springframework.web.bind.annotation.RequestMethod;
  66. import org.springframework.web.bind.annotation.RequestParam;
  67. import org.springframework.web.multipart.MultipartFile;
  68. import org.springframework.web.servlet.ModelAndView;
  69.  
  70. @Controller
  71. public class StudentController {
  72.  
  73. @Autowired
  74. private ServletContext servletContext;
  75.  
  76. // Constructor based Dependency Injection
  77. private StudentService studentService;
  78.  
  79. public StudentController() {
  80.  
  81. }
  82.  
  83. @Autowired
  84. public StudentController(StudentService studentService) {
  85. this.studentService = studentService;
  86. }
  87.  
  88.  
  89.  
  90.  
  91. @RequestMapping(value = "/allStudents", method = {RequestMethod.GET, RequestMethod.POST})
  92.  
  93. public ModelAndView displayAllUser() {
  94. System.out.println("User Page Requested : All Students");
  95. ModelAndView mv = new ModelAndView();
  96. List<Student> studentList = studentService.getAllStudents();
  97. mv.addObject("studentList", studentList);
  98. mv.setViewName("allStudents");
  99. return mv;
  100. }
  101.  
  102. @RequestMapping(value = "/addStudent", method = RequestMethod.GET)
  103. public ModelAndView displayNewUserForm() {
  104. ModelAndView mv = new ModelAndView("addStudent");
  105. mv.addObject("headerMessage", "Add Student Details");
  106. mv.addObject("student", new Student());
  107. return mv;
  108. }
  109.  
  110. @PostMapping(value = "/addStudent")
  111. public String saveNewStudent(@RequestParam("name") @NonNull String name,
  112. @RequestParam("surname") @NonNull String surname,
  113. @RequestParam("avatar") MultipartFile file)
  114. throws IOException {
  115.  
  116. Student student = new Student();
  117. student.setSurname(surname);
  118. student.setName(name);
  119.  
  120. if (file != null && !file.isEmpty()) {
  121. student.setAvatar(studentService.saveAvatarImage(file).getName());
  122. }
  123.  
  124. studentService.saveStudent(student);
  125. return "redirect:/allStudents";
  126. }
  127.  
  128. @GetMapping(value = "/editStudent/{id}")
  129. public ModelAndView displayEditUserForm(@PathVariable Long id) {
  130. ModelAndView mv = new ModelAndView("editStudent");
  131. Student student = studentService.getStudentById(id);
  132. mv.addObject("headerMessage", "Редактирование студента");
  133. mv.addObject("student", student);
  134. return mv;
  135. }
  136.  
  137. @PostMapping(value = "/editStudent")
  138. public String saveEditedUser(
  139. @RequestParam("id") Long id,
  140. @RequestParam("name") String name,
  141. @RequestParam("surname") String surname,
  142. @RequestParam("avatar") MultipartFile file) {
  143.  
  144. try {
  145.  
  146. studentService.updateStudent(name, surname, file, studentService.getStudentById(id));
  147.  
  148. } catch (FileSystemException ex) {
  149. ex.printStackTrace();
  150. } catch (IOException e) {
  151. return "redirect:/error";
  152. }
  153.  
  154. return "redirect:/allStudents";
  155. }
  156.  
  157. @GetMapping(value = "/deleteStudent/{id}")
  158. public ModelAndView deleteUserById(@PathVariable Long id) {
  159. studentService.deleteStudentById(id);
  160. ModelAndView mv = new ModelAndView("redirect:/allStudents");
  161.  
  162. return mv;
  163.  
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement