1+ package com .baeldung .spring .mybatis .controller ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Controller ;
5+ import org .springframework .ui .Model ;
6+ import org .springframework .ui .ModelMap ;
7+ import org .springframework .validation .BindingResult ;
8+ import org .springframework .validation .annotation .Validated ;
9+ import org .springframework .web .bind .annotation .ModelAttribute ;
10+ import org .springframework .web .bind .annotation .RequestMapping ;
11+ import org .springframework .web .bind .annotation .RequestMethod ;
12+ import org .springframework .web .bind .annotation .SessionAttributes ;
13+
14+ import com .baeldung .spring .mybatis .model .Student ;
15+ import com .baeldung .spring .mybatis .model .StudentLogin ;
16+ import com .baeldung .spring .mybatis .service .StudentService ;
17+
18+ @ Controller
19+ @ SessionAttributes ("student" )
20+ public class StudentController {
21+
22+ @ Autowired
23+ private StudentService studentService ;
24+
25+ @ RequestMapping (value = "/signup" , method = RequestMethod .GET )
26+ public String signup (Model model ) {
27+ Student student = new Student ();
28+ model .addAttribute ("student" , student );
29+ return "signup" ;
30+ }
31+
32+ @ RequestMapping (value = "/signup" , method = RequestMethod .POST )
33+ public String signup (@ Validated @ ModelAttribute ("student" ) Student student , BindingResult result , ModelMap model ) {
34+ if (studentService .getStudentByUserName (student .getUserName ())) {
35+ model .addAttribute ("message" , "User Name exists. Try another user name" );
36+ return "signup" ;
37+ } else {
38+ studentService .insertStudent (student );
39+ model .addAttribute ("message" , "Saved student details" );
40+ return "redirect:login.html" ;
41+ }
42+ }
43+
44+ @ RequestMapping (value = "/login" , method = RequestMethod .GET )
45+ public String login (Model model ) {
46+ StudentLogin studentLogin = new StudentLogin ();
47+ model .addAttribute ("studentLogin" , studentLogin );
48+ return "login" ;
49+ }
50+
51+ @ RequestMapping (value = "/login" , method = RequestMethod .POST )
52+ public String login (@ ModelAttribute ("studentLogin" ) StudentLogin studentLogin , BindingResult result , ModelMap model ) {
53+ boolean found = studentService .getStudentByLogin (studentLogin .getUserName (), studentLogin .getPassword ());
54+ if (found ) {
55+ return "success" ;
56+ } else {
57+ return "failure" ;
58+ }
59+ }
60+ }
0 commit comments