Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package com.axis.batch197.controller;
  2.  
  3. import java.util.Date;
  4.  
  5. import javax.validation.Valid;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.http.ResponseEntity;
  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.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.servlet.ModelAndView;
  16.  
  17. import com.axis.batch197.model.XAddressBookModel;
  18. import com.axis.batch197.repository.XAddressBookRepo;
  19. import com.axis.batch197.service.UserService;
  20.  
  21.  
  22. @Controller
  23. public class AuthenticationController {
  24.  
  25. @Autowired
  26. UserService userService;
  27.  
  28. @Autowired
  29. private XAddressBookRepo userRepository;
  30.  
  31. @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
  32. public ModelAndView login() {
  33. ModelAndView modelAndView = new ModelAndView();
  34. // List<XAddressBookModel> address = repo.search(email, abpwd);
  35. modelAndView.setViewName("login");
  36. return modelAndView;
  37. }
  38.  
  39. @RequestMapping(value = "/register", method = RequestMethod.GET)
  40. public ModelAndView register() {
  41. ModelAndView modelAndView = new ModelAndView();
  42. XAddressBookModel pengguna = new XAddressBookModel();
  43. modelAndView.addObject("pengguna", pengguna);
  44. modelAndView.setViewName("register"); // resources/template/register.html
  45. return modelAndView;
  46. }
  47.  
  48. @RequestMapping(value = "/index", method = RequestMethod.GET)
  49. public ModelAndView home() {
  50. ModelAndView modelAndView = new ModelAndView();
  51. modelAndView.setViewName("index"); // resources/template/home.html
  52. return modelAndView;
  53. }
  54.  
  55.  
  56. @RequestMapping(value="/register", method=RequestMethod.POST)
  57. public ModelAndView registerPengguna(@Valid @ModelAttribute("pengguna") XAddressBookModel pengguna, BindingResult bindingResult, ModelMap modelMap) {
  58. ModelAndView modelAndView = new ModelAndView();
  59. // Check for the validations
  60. if(bindingResult.hasErrors()) {
  61. modelAndView.addObject("successMessage", "Please correct the errors in form!");
  62. modelMap.addAttribute("bindingResult", bindingResult);
  63. }
  64. else if(userService.isUserAlreadyPresent(pengguna)){
  65. modelAndView.addObject("successMessage", "user already exists!");
  66. }
  67. // we will save the user if, no binding errors
  68. else {
  69. userService.savePengguna(pengguna);
  70. // pengguna.setModifiedOn(new Date());
  71.  
  72.  
  73.  
  74. modelAndView.addObject("successMessage", "User is registered successfully!");
  75. }
  76. modelAndView.addObject("pengguna", new XAddressBookModel());
  77. modelAndView.setViewName("register");
  78. return modelAndView;
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement