|
| 1 | +package com.letstartcoding.springbootexample.controller; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import javax.validation.Valid; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.stereotype.Controller; |
| 10 | +import org.springframework.ui.ModelMap; |
| 11 | +import org.springframework.validation.BindingResult; |
| 12 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 16 | +import org.springframework.web.servlet.ModelAndView; |
| 17 | +import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
| 18 | + |
| 19 | +import com.letstartcoding.springbootexample.model.Student; |
| 20 | +import com.letstartcoding.springbootexample.service.StudentService; |
| 21 | + |
| 22 | +@Controller |
| 23 | + |
| 24 | +public class StudentEnrollmentController { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private StudentService studentService; |
| 28 | + |
| 29 | + @RequestMapping(value ="/enroll",method = RequestMethod.GET) |
| 30 | + public String newRegistration(ModelMap model) { |
| 31 | + Student student = new Student(); |
| 32 | + model.addAttribute("student", student); |
| 33 | + return "enroll"; |
| 34 | + } |
| 35 | + |
| 36 | + @RequestMapping(value ="/save",method = RequestMethod.POST) |
| 37 | + public String saveRegistration(@Valid Student student, |
| 38 | + BindingResult result, ModelMap model,RedirectAttributes redirectAttributes) { |
| 39 | + |
| 40 | + if (result.hasErrors()) { |
| 41 | + return "enroll";//will redirect to viewemp request mapping |
| 42 | + } |
| 43 | + studentService.save(student); |
| 44 | + //redirectAttributes.addFlashAttribute("message", "Student " + student.getFirstName()+" "+student.getLastName() + " saved"); |
| 45 | + return "redirect:/viewstudents/1";//will redirect to viewemp request mapping |
| 46 | + } |
| 47 | + |
| 48 | + @RequestMapping("/viewstudents") |
| 49 | + public ModelAndView viewstudents(){ |
| 50 | + List<Student> list=studentService.getAllStudents(); |
| 51 | + return new ModelAndView("viewstudents","list",list); |
| 52 | + } |
| 53 | + |
| 54 | + /* It opens the student list for the given page id */ |
| 55 | + @RequestMapping(value="/viewstudents/{pageid}") |
| 56 | + public ModelAndView edit(@PathVariable int pageid){ |
| 57 | + int total=2; |
| 58 | + if(pageid==1){} |
| 59 | + else{ |
| 60 | + pageid=(pageid-1)*total+1; |
| 61 | + } |
| 62 | + List<Student> list=studentService.getStudentsByPage(pageid,total); |
| 63 | + |
| 64 | + return new ModelAndView("viewstudents","list",list); |
| 65 | + } |
| 66 | + |
| 67 | + /* It opens the record for the given id in editstudent page */ |
| 68 | + @RequestMapping(value="/editstudent/{id}") |
| 69 | + public String edit(@PathVariable int id,ModelMap model){ |
| 70 | + Student student=studentService.getStudentById(id); |
| 71 | + model.addAttribute("student", student); |
| 72 | + return "editstudent"; |
| 73 | + |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /* It updates record for the given id in editstudent page and redirects to /viewstudents */ |
| 78 | + @RequestMapping(value="/editsave",method = RequestMethod.POST) |
| 79 | + public ModelAndView editsave(@ModelAttribute("student") Student emp){ |
| 80 | + System.out.println("id is"+emp.getId()); |
| 81 | + studentService.update(emp); |
| 82 | + return new ModelAndView("redirect:/viewstudents/1"); |
| 83 | + } |
| 84 | + |
| 85 | + /* It deletes record for the given id and redirects to /viewstudents */ |
| 86 | + @RequestMapping(value="/deletestudent/{id}",method = RequestMethod.GET) |
| 87 | + public ModelAndView delete(@PathVariable int id){ |
| 88 | + studentService.delete(id); |
| 89 | + return new ModelAndView("redirect:/viewstudents/1"); |
| 90 | + } |
| 91 | + |
| 92 | + /* It deletes record for the given id and redirects to /viewstudents */ |
| 93 | + @RequestMapping(value="/delete",method = RequestMethod.GET) |
| 94 | + public ModelAndView delete(){ |
| 95 | + studentService.delete(); |
| 96 | + return new ModelAndView("redirect:/enroll"); |
| 97 | + } |
| 98 | + |
| 99 | + @ModelAttribute("sections") |
| 100 | + public List<String> initializeSections() { |
| 101 | + |
| 102 | + List<String> sections = new ArrayList<String>(); |
| 103 | + sections.add("Graduate"); |
| 104 | + sections.add("Post Graduate"); |
| 105 | + sections.add("Research"); |
| 106 | + return sections; |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * Method used to populate the country list in view. Note that here you can |
| 111 | + * call external systems to provide real data. |
| 112 | + */ |
| 113 | + @ModelAttribute("countries") |
| 114 | + public List<String> initializeCountries() { |
| 115 | + |
| 116 | + List<String> countries = new ArrayList<String>(); |
| 117 | + countries.add("INDIA"); |
| 118 | + countries.add("USA"); |
| 119 | + countries.add("CANADA"); |
| 120 | + countries.add("FRANCE"); |
| 121 | + countries.add("GERMANY"); |
| 122 | + countries.add("ITALY"); |
| 123 | + countries.add("OTHER"); |
| 124 | + return countries; |
| 125 | + } |
| 126 | + |
| 127 | + /* |
| 128 | + * Method used to populate the subjects list in view. Note that here you can |
| 129 | + * call external systems to provide real data. |
| 130 | + */ |
| 131 | + @ModelAttribute("subjects") |
| 132 | + public List<String> initializeSubjects() { |
| 133 | + |
| 134 | + List<String> subjects = new ArrayList<String>(); |
| 135 | + subjects.add("Physics"); |
| 136 | + subjects.add("Chemistry"); |
| 137 | + subjects.add("Life Science"); |
| 138 | + subjects.add("Political Science"); |
| 139 | + subjects.add("Computer Science"); |
| 140 | + subjects.add("Mathmatics"); |
| 141 | + return subjects; |
| 142 | + } |
| 143 | + |
| 144 | + /* |
| 145 | + * Method used to populate the Section list in view. Note that here you can |
| 146 | + * call external systems to provide real data. |
| 147 | + */ |
| 148 | + @ModelAttribute("pageCount") |
| 149 | + public List<String> initializePageCount() { |
| 150 | + int total=2; |
| 151 | + List<String> pageCount = new ArrayList<String>(); |
| 152 | + int count=studentService.count(); |
| 153 | + int result=((count/total)+ (count%total)); |
| 154 | + for(int k=0;k<result;k++) { |
| 155 | + pageCount.add(new Integer(k).toString()); |
| 156 | + } |
| 157 | + |
| 158 | + return pageCount; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments