Package contains the controller classes of the application.
Package net.devmanuals.controller
This package contains the controller classes of this application. Given below the controller classes structure followed by code with their description :
AddQuestionController.java
This controller class is used to handle the question addition in the application.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.validation.Valid; import net.devmanuals.model.Admin; import net.devmanuals.model.QuestionOptions; import net.devmanuals.model.Questions; import net.devmanuals.service.QuestionOptionsService; import net.devmanuals.service.QuestionsService; import net.devmanuals.service.LanguageService; import net.devmanuals.validators.AddQuestionForm; import net.devmanuals.validators.AddQuestionValidator; @Controller @RequestMapping("/admin/addquestion") public class AddQuestionController { @Autowired private LanguageService languageService; @Autowired private AddQuestionValidator addQuestionValidator; @Autowired private QuestionsService questionsService; @Autowired private QuestionOptionsService questionOptionsService; @RequestMapping(method = RequestMethod.GET) public String showQuestionController(Map model, HttpServletRequest request, HttpSession session) { if ((session.getAttribute("adminEmail")) == null) { Admin admin = new Admin(); model.put("admin", admin); return "/admin/adminlogin"; } AddQuestionForm addQuestionForm = new AddQuestionForm(); String pageHeading = "Add Question"; // if fined questionId then edit Question if (request.getParameter("questionId") != null) { pageHeading = "Edit Question"; Long questionId = Long .parseLong(request.getParameter("questionId")); List questionlist = questionsService .getQuestionsByQuestionId(questionId); if (questionlist != null && questionlist.size() > 0) { Questions questions = (Questions) questionlist.get(0); addQuestionForm.setQuestionId(questions.getQuestionId()); addQuestionForm.setLanguageId(questions.getLanguageId()); addQuestionForm.setQuestion(questions.getQuestion()); List questionoptionslist = questionOptionsService .getQuestionOptionsByQuestionId(questionId); if (questionoptionslist != null && questionoptionslist.size() > 0) { QuestionOptions questionOptions1 = (QuestionOptions) questionoptionslist .get(0); QuestionOptions questionOptions2 = (QuestionOptions) questionoptionslist .get(1); QuestionOptions questionOptions3 = (QuestionOptions) questionoptionslist .get(2); QuestionOptions questionOptions4 = (QuestionOptions) questionoptionslist .get(3); addQuestionForm.setOption1(questionOptions1 .getAnsDescription()); addQuestionForm.setOption2(questionOptions2 .getAnsDescription()); addQuestionForm.setOption3(questionOptions3 .getAnsDescription()); addQuestionForm.setOption4(questionOptions4 .getAnsDescription()); if ((questionOptions1.getQuesOpId()).compareTo(questions .getRightOption()) == 0) { addQuestionForm.setRightOption(new Long(1)); } if ((questionOptions2.getQuesOpId()).compareTo(questions .getRightOption()) == 0) { addQuestionForm.setRightOption(new Long(2)); } if ((questionOptions3.getQuesOpId()).compareTo(questions .getRightOption()) == 0) { addQuestionForm.setRightOption(new Long(3)); } if ((questionOptions4.getQuesOpId()).compareTo(questions .getRightOption()) == 0) { addQuestionForm.setRightOption(new Long(4)); } } } } model.put("pageHeading", pageHeading); model.put("addQuestionForm", addQuestionForm); model.put("languagelist", languageService.listLanguages()); return "/admin/addquestion"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView processQuestionController( @Valid AddQuestionForm addQuestionForm, BindingResult result, Map model, HttpSession session) { if ((session.getAttribute("adminEmail")) == null) { return new ModelAndView("redirect:adminlogin.html"); } addQuestionValidator.validate(addQuestionForm, result); String pageHeading = "Add Question"; if (addQuestionForm.getQuestionId() != null) pageHeading = "Edit Question"; // check all error if (result.hasErrors()) { model.put("pageHeading", pageHeading); model.put("languagelist", languageService.listLanguages()); ModelAndView modelandview = new ModelAndView(); return new ModelAndView("/admin/addquestion"); } else { Questions questions = new Questions(); questions.setLanguageId(addQuestionForm.getLanguageId()); questions.setQuestion(addQuestionForm.getQuestion()); // Add Question and Answer if (addQuestionForm.getQuestionId() == null) { questions.setRightOption(new Long(0)); questionsService.saveQuestions(questions); QuestionOptions questionOptions1 = new QuestionOptions(); questionOptions1 .setAnsDescription(addQuestionForm.getOption1()); questionOptions1.setQuestionId(questions.getQuestionId()); questionOptionsService.saveQuestionOptions(questionOptions1); QuestionOptions questionOptions2 = new QuestionOptions(); questionOptions2 .setAnsDescription(addQuestionForm.getOption2()); questionOptions2.setQuestionId(questions.getQuestionId()); questionOptionsService.saveQuestionOptions(questionOptions2); QuestionOptions questionOptions3 = new QuestionOptions(); questionOptions3 .setAnsDescription(addQuestionForm.getOption3()); questionOptions3.setQuestionId(questions.getQuestionId()); questionOptionsService.saveQuestionOptions(questionOptions3); QuestionOptions questionOptions4 = new QuestionOptions(); questionOptions4 .setAnsDescription(addQuestionForm.getOption4()); questionOptions4.setQuestionId(questions.getQuestionId()); questionOptionsService.saveQuestionOptions(questionOptions4); int comp1 = 1; int comp2 = 1; int comp3 = 1; int comp4 = 1; comp1 = (addQuestionForm.getRightOption()) .compareTo(new Long(1)); comp2 = (addQuestionForm.getRightOption()) .compareTo(new Long(2)); comp3 = (addQuestionForm.getRightOption()) .compareTo(new Long(3)); comp4 = (addQuestionForm.getRightOption()) .compareTo(new Long(4)); if (comp1 == 0) { questions.setRightOption(questionOptions1.getQuesOpId()); questionsService.updateQuestions(questions); } if (comp2 == 0) { questions.setRightOption(questionOptions2.getQuesOpId()); questionsService.updateQuestions(questions); } if (comp3 == 0) { questions.setRightOption(questionOptions3.getQuesOpId()); questionsService.updateQuestions(questions); } if (comp4 == 0) { questions.setRightOption(questionOptions4.getQuesOpId()); questionsService.updateQuestions(questions); } } else { // update Question and Answer questions.setQuestionId(addQuestionForm.getQuestionId()); List questionoptionslist = questionOptionsService .getQuestionOptionsByQuestionId(addQuestionForm .getQuestionId()); if (questionoptionslist != null && questionoptionslist.size() > 0) { QuestionOptions questionOptions1 = (QuestionOptions) questionoptionslist .get(0); QuestionOptions questionOptions2 = (QuestionOptions) questionoptionslist .get(1); QuestionOptions questionOptions3 = (QuestionOptions) questionoptionslist .get(2); QuestionOptions questionOptions4 = (QuestionOptions) questionoptionslist .get(3); questionOptions1.setAnsDescription(addQuestionForm .getOption1()); questionOptions2.setAnsDescription(addQuestionForm .getOption2()); questionOptions3.setAnsDescription(addQuestionForm .getOption3()); questionOptions4.setAnsDescription(addQuestionForm .getOption4()); questionOptionsService .updateQuestionOptions(questionOptions1); questionOptionsService .updateQuestionOptions(questionOptions2); questionOptionsService .updateQuestionOptions(questionOptions3); questionOptionsService .updateQuestionOptions(questionOptions4); if ((addQuestionForm.getRightOption()) .compareTo(new Long(1)) == 0) { questions .setRightOption(questionOptions1.getQuesOpId()); } if ((addQuestionForm.getRightOption()) .compareTo(new Long(2)) == 0) { questions .setRightOption(questionOptions2.getQuesOpId()); } if ((addQuestionForm.getRightOption()) .compareTo(new Long(3)) == 0) { questions .setRightOption(questionOptions3.getQuesOpId()); } if ((addQuestionForm.getRightOption()) .compareTo(new Long(4)) == 0) { questions .setRightOption(questionOptions4.getQuesOpId()); } questionsService.updateQuestions(questions); } } } return new ModelAndView("redirect:questionlist.html"); } }
AdminLoginController.java
This controller class handles the admin login in the application :
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.List; import java.util.Map; import javax.servlet.http.HttpSession; import javax.validation.Valid; import net.devmanuals.model.Admin; import net.devmanuals.model.User; import net.devmanuals.service.AdminService; import net.devmanuals.validators.AdminLoginValidator; @Controller @RequestMapping("/admin/adminlogin") public class AdminLoginController { @Autowired private AdminLoginValidator adminLoginValidator; @Autowired private AdminService adminService; @RequestMapping(method = RequestMethod.GET) public ModelAndView showAdminLogin(Map model, HttpSession session) { if (session.getAttribute("adminEmail") == null) { Admin admin = new Admin(); model.put("admin", admin); return new ModelAndView("/admin/adminlogin"); } else { return new ModelAndView("redirect:addquestion.html"); } } @RequestMapping(method = RequestMethod.POST) public ModelAndView processAdminLogin(@Valid Admin admin, BindingResult result, Map model, HttpSession session) { adminLoginValidator.validate(admin, result); if (result.hasErrors()) { return new ModelAndView("/admin/adminlogin"); } List adminlist = adminService.getAdminByAdminEmail(admin .getAdminEmail()); if (adminlist != null && adminlist.size() > 0) { Admin admin1 = new Admin(); admin1 = (Admin) adminlist.get(0); session.setAttribute("adminName", admin1.getAdminName()); } session.setAttribute("adminEmail", admin.getAdminEmail()); return new ModelAndView("redirect:addquestion.html"); } }
AdminLogOutController.java
This controller class class handle the admin logout.
package net.devmanuals.controller; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/admin/logout") public class AdminLogOutController { @RequestMapping(method = RequestMethod.GET) public ModelAndView logOut(HttpSession session) { session.invalidate(); return new ModelAndView("redirect:adminlogin.html"); } }
DeleteQuestionController.java
This controller class handle the deletion process of questions in the application
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import net.devmanuals.model.Admin; import net.devmanuals.model.Questions; import net.devmanuals.service.QuestionOptionsService; import net.devmanuals.service.QuestionsService; import net.devmanuals.service.LanguageService; @Controller @RequestMapping(value = "/admin/deletequestion") public class DeleteQuestionController { @Autowired private LanguageService languageService; @Autowired private QuestionsService questionsService; @Autowired private QuestionOptionsService questionOptionsService; @RequestMapping(method = RequestMethod.GET) public ModelAndView showDeleteQuestion(Map model, HttpServletRequest request, HttpSession session) { if ((session.getAttribute("adminEmail")) == null) { // Admin admin=new Admin(); // model.put("admin",admin); // return "/admin/adminlogin"; return new ModelAndView("redirect:adminlogin.html"); } model.put("languagelist", languageService.listLanguages()); String questionIdInString = request.getParameter("questionId"); if (questionIdInString != null && (!questionIdInString.equals(""))) { Questions questions = new Questions(); questions.setQuestionId(Long.parseLong(questionIdInString)); questionsService.deleteQuestions(questions); questionOptionsService.deleteQuestionOptionsByQuestionId(Long .parseLong(questionIdInString)); } return new ModelAndView("redirect:questionlist.html?languageId=" + request.getParameter("languageId")); } }
QuestionListController.java
This controller class handles the display of List of Questions added so far to the admin.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import net.devmanuals.service.LanguageService; import net.devmanuals.service.QuestionOptionsService; import net.devmanuals.service.QuestionsService; import net.devmanuals.model.QuestionOptions; import net.devmanuals.model.Questions; import net.devmanuals.model.Language; import net.devmanuals.validators.QuestionPaperCommand; @Controller @RequestMapping(value = "/admin/questionlist") public class QuestionListController { @Autowired private QuestionsService questionsService; @Autowired private QuestionOptionsService questionOptionsService; @Autowired private LanguageService languageService; @RequestMapping(method = RequestMethod.GET) public ModelAndView showQuestionList(Map model, HttpServletRequest request, HttpSession session) { if ((session.getAttribute("adminEmail")) == null) { return new ModelAndView("redirect:adminlogin.html"); } List allQuestionList = new ArrayList(); List questionlist = new ArrayList(); String languageIdInString = request.getParameter("languageId"); if (languageIdInString != null && (!languageIdInString.equals(""))) { questionlist = questionsService.getQuestionsByLanguageId(Long .parseLong(languageIdInString)); model.put("languageId", Long.parseLong(languageIdInString)); } else { questionlist = questionsService.listQuestions(); } for (int i = 0; i < questionlist.size(); i++) { QuestionPaperCommand questionPaperCommand = new QuestionPaperCommand(); List questionOptionsList = new ArrayList(); Questions questions = new Questions(); questions = (Questions) questionlist.get(i); questionPaperCommand.setQuestionId(questions.getQuestionId()); questionPaperCommand.setQuestion(questions.getQuestion()); List languagelist = languageService .getLanguageByLanguageId(questions.getLanguageId()); if (languagelist != null && languagelist.size() > 0) { Language language = (Language) languagelist.get(0); questionPaperCommand .setLanguageName(language.getLanguageName()); } questionOptionsList = questionOptionsService .getQuestionOptionsByQuestionId(questions.getQuestionId()); if (questionOptionsList != null && questionOptionsList.size() > 0) { QuestionOptions questionOptions1 = (QuestionOptions) questionOptionsList .get(0); QuestionOptions questionOptions2 = (QuestionOptions) questionOptionsList .get(1); QuestionOptions questionOptions3 = (QuestionOptions) questionOptionsList .get(2); QuestionOptions questionOptions4 = (QuestionOptions) questionOptionsList .get(3); if (questionOptions1 != null) { questionPaperCommand.setOption1Id(questionOptions1 .getQuesOpId()); questionPaperCommand.setOption1(questionOptions1 .getAnsDescription()); if (((questionOptions1.getQuesOpId()).compareTo(questions .getRightOption())) == 0) { questionPaperCommand.setRightAnswer(questionOptions1 .getAnsDescription()); } } if (questionOptions2 != null) { questionPaperCommand.setOption2Id(questionOptions2 .getQuesOpId()); questionPaperCommand.setOption2(questionOptions2 .getAnsDescription()); if (((questionOptions2.getQuesOpId()).compareTo(questions .getRightOption())) == 0) { questionPaperCommand.setRightAnswer(questionOptions2 .getAnsDescription()); } } if (questionOptions3 != null) { questionPaperCommand.setOption3Id(questionOptions3 .getQuesOpId()); questionPaperCommand.setOption3(questionOptions3 .getAnsDescription()); if (((questionOptions3.getQuesOpId()).compareTo(questions .getRightOption())) == 0) { questionPaperCommand.setRightAnswer(questionOptions3 .getAnsDescription()); } } if (questionOptions4 != null) { questionPaperCommand.setOption4Id(questionOptions4 .getQuesOpId()); questionPaperCommand.setOption4(questionOptions4 .getAnsDescription()); if (((questionOptions4.getQuesOpId()).compareTo(questions .getRightOption())) == 0) { questionPaperCommand.setRightAnswer(questionOptions4 .getAnsDescription()); } } questionPaperCommand.setRightOption(questions.getRightOption()); } allQuestionList.add(questionPaperCommand); } model.put("allQuestionList", allQuestionList); return new ModelAndView("admin/questionlist"); } }
QuestionPaperController.java
This controller class handles the display of the Questions to the logged user.
package net.devmanuals.controller; import java.util.ArrayList; import java.util.Map; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import net.devmanuals.service.QuestionOptionsService; import net.devmanuals.service.QuestionsService; import net.devmanuals.model.Admin; import net.devmanuals.model.Questions; import net.devmanuals.model.QuestionOptions; import net.devmanuals.model.User; import net.devmanuals.validators.QuestionPaperCommand; @Controller @RequestMapping("/user/questionpaper") public class QuestionPaperController { @Autowired private QuestionsService questionsService; @Autowired private QuestionOptionsService questionOptionsService; @RequestMapping(method = RequestMethod.GET) public String showQuestionPaper(HttpServletRequest request, Map model, HttpSession session) { if ((session.getAttribute("userEmail")) == null) { User user = new User(); model.put("user", user); return "/user/userlogin"; } List questionPaperList = new ArrayList(); List questonIdList = new ArrayList(); String languageIdInString = request.getParameter("languageId"); if (languageIdInString != null) { Long languageId = Long.parseLong(languageIdInString); QuestionPaperCommand questionPaperCommand1 = new QuestionPaperCommand(); // List // questionlist=questionsService.getQuestionsByLanguageId(languageId); List questionlist = questionsService .getTenRandomQuestionsByLanguageId(languageId); for (int i = 0; i < questionlist.size(); i++) { List questionOptionsList = new ArrayList(); QuestionPaperCommand questionPaperCommand = new QuestionPaperCommand(); Questions questions = new Questions(); questions = (Questions) questionlist.get(i); // set questonId in questonIdList questonIdList.add(i, questions.getQuestionId()); questionPaperCommand.setQuestionId(questions.getQuestionId()); questionPaperCommand.setQuestion(questions.getQuestion()); questionOptionsList = questionOptionsService .getQuestionOptionsByQuestionId(questions .getQuestionId()); if (questionOptionsList != null && questionOptionsList.size() > 0) { QuestionOptions questionOptions1 = (QuestionOptions) questionOptionsList .get(0); QuestionOptions questionOptions2 = (QuestionOptions) questionOptionsList .get(1); QuestionOptions questionOptions3 = (QuestionOptions) questionOptionsList .get(2); QuestionOptions questionOptions4 = (QuestionOptions) questionOptionsList .get(3); if (questionOptions1 != null) { questionPaperCommand.setOption1Id(questionOptions1 .getQuesOpId()); questionPaperCommand.setOption1(questionOptions1 .getAnsDescription()); } if (questionOptions2 != null) { questionPaperCommand.setOption2Id(questionOptions2 .getQuesOpId()); questionPaperCommand.setOption2(questionOptions2 .getAnsDescription()); } if (questionOptions3 != null) { questionPaperCommand.setOption3Id(questionOptions3 .getQuesOpId()); questionPaperCommand.setOption3(questionOptions3 .getAnsDescription()); } if (questionOptions4 != null) { questionPaperCommand.setOption4Id(questionOptions4 .getQuesOpId()); questionPaperCommand.setOption4(questionOptions4 .getAnsDescription()); } } questionPaperList.add(questionPaperCommand); } } session.setAttribute("questonIdList", questonIdList); model.put("questionPaperList", questionPaperList); return "/user/questionpaper"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView processQuestionPaper(Map model, HttpServletRequest request, HttpSession session) { String[] question = request.getParameterValues("question"); int rightAnswer = 0; for (int i = 0; i < question.length; i++) { if (request.getParameter("quesNum[" + i + "]") != null && request.getParameter("option[" + i + "]") != null) { List<Questions> questionlist = new ArrayList<Questions>(); Long questionId = Long.parseLong(request .getParameter("quesNum[" + i + "]")); Long rightOption = Long.parseLong(request .getParameter("option[" + i + "]")); questionlist = questionsService .getQuestionsByQuestionId(questionId); if (questionlist != null && questionlist.size() > 0) { Questions questions = new Questions(); questions = questionlist.get(0); int comp = rightOption .compareTo(questions.getRightOption()); if (comp == 0) { rightAnswer++; } } } } session.setAttribute("totalQuestion", question.length); session.setAttribute("rightAnswer", rightAnswer); session.setAttribute("wongAnswer", (question.length) - rightAnswer); return new ModelAndView("redirect:questionpaperresult.html"); } }
QuestionPaperResultController.java
This controller class handles the display of result after submission of question paper. It also display the correct result option of each question.
package net.devmanuals.controller; import java.util.ArrayList; import java.util.List; import net.devmanuals.model.QuestionOptions; import net.devmanuals.model.Questions; import net.devmanuals.model.User; import net.devmanuals.service.QuestionOptionsService; import net.devmanuals.service.QuestionsService; import net.devmanuals.validators.QuestionPaperCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.util.Map; import javax.servlet.http.HttpSession; @Controller @RequestMapping(value = "/user/questionpaperresult") public class QuestionPaperResultController { @Autowired private QuestionsService questionsService; @Autowired private QuestionOptionsService questionOptionsService; @RequestMapping(method = RequestMethod.GET) public String showQuestionPaperResult(Map model, HttpSession session) { if ((session.getAttribute("userEmail")) == null) { User user = new User(); model.put("user", user); return "/user/userlogin"; } List<QuestionPaperCommand> questionPaperList = new ArrayList<QuestionPaperCommand>(); List questonIdList = (List) session.getAttribute("questonIdList"); List questionlist = new ArrayList(); for (int i = 0; i < questonIdList.size(); i++) { Long questionId = (Long) questonIdList.get(i); questionlist = questionsService .getQuestionsByQuestionId(questionId); if (questionlist != null && questionlist.size() > 0) { for (int j = 0; j < questionlist.size(); j++) { QuestionPaperCommand questionPaperCommand = new QuestionPaperCommand(); List questionOptionsList = new ArrayList(); Questions questions = new Questions(); questions = (Questions) questionlist.get(j); questionPaperCommand.setQuestionId(questions .getQuestionId()); questionPaperCommand.setQuestion(questions.getQuestion()); // questionOptionsList questionOptionsList = questionOptionsService .getQuestionOptionsByQuesOpId(questions .getRightOption()); if (questionOptionsList != null && questionOptionsList.size() > 0) { QuestionOptions questionOptions = (QuestionOptions) questionOptionsList .get(0); questionPaperCommand.setOption1(questionOptions .getAnsDescription()); } questionPaperList.add(questionPaperCommand); } } } model.put("questionPaperList", questionPaperList); model.put("totalQuestion", session.getAttribute("totalQuestion")); model.put("rightAnswer", session.getAttribute("rightAnswer")); model.put("wongAnswer", session.getAttribute("wongAnswer")); session.removeAttribute("totalQuestion"); session.removeAttribute("rightAnswer"); session.removeAttribute("wongAnswer"); return "/user/questionpaperresult"; } }
SearchQuestionController.java
This controller class handles the display of question according to search option selected.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import javax.servlet.http.HttpSession; import javax.validation.Valid; import net.devmanuals.model.Admin; import net.devmanuals.model.Language; import net.devmanuals.service.LanguageService; @Controller @RequestMapping(value="/admin/searchquestion") public class SearchQuestionController { @Autowired private LanguageService languageService; @RequestMapping(method=RequestMethod.GET) public String showSearchQuestion(Map model,HttpSession session){ if((session.getAttribute("adminEmail"))==null){ Admin admin=new Admin(); model.put("admin",admin); return "/admin/adminlogin"; } Language language=new Language(); model.put("language", language); model.put("languagelist", languageService.listLanguages()); return "/admin/searchquestion"; } @RequestMapping(method=RequestMethod.POST) public ModelAndView processSearchQuestion(@Valid Language language,Map model){ Long languageId=language.getLanguageId(); if(languageId==-1){ model.put("languagelist", languageService.listLanguages()); model.put("errormessage", "Select Language"); return new ModelAndView("admin/searchquestion"); } return new ModelAndView("redirect:questionlist.html?languageId="+language.getLanguageId()); } }
SelectPaperController.java
This controller class handles the display of selected paper questions to the user.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import javax.servlet.http.HttpSession; import javax.validation.Valid; import net.devmanuals.model.Language; import net.devmanuals.model.User; import net.devmanuals.service.LanguageService; @Controller @RequestMapping("/user/selectpaper") public class SelectPaperController { @Autowired private LanguageService languageService; @RequestMapping(method = RequestMethod.GET) public String showSelectPaper(Map model, HttpSession session) { if ((session.getAttribute("userEmail")) == null) { User user = new User(); model.put("user", user); return "/user/userlogin"; } Language language = new Language(); model.put("language", language); model.put("languagelist", languageService.listLanguages()); return "/user/selectpaper"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView processSelectPaper(@Valid Language language, Map model, HttpSession session) { if ((session.getAttribute("userEmail")) == null) { return new ModelAndView("redirect:userlogin.html"); } Long languageId = language.getLanguageId(); if (languageId == -1) { model.put("languagelist", languageService.listLanguages()); model.put("errormessage", "Select Language"); return new ModelAndView("/user/selectpaper"); } return new ModelAndView("redirect:questionpaper.html?languageId=" + language.getLanguageId()); } }
UserLoginController.java
This controller class handles the user login.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import java.util.List; import javax.servlet.http.HttpSession; import javax.validation.Valid; import net.devmanuals.model.User; import net.devmanuals.service.UserService; import net.devmanuals.validators.UserLoginValidator; @Controller @RequestMapping("/user/userlogin") public class UserLoginController { @Autowired private UserLoginValidator userLoginValidator; @Autowired private UserService userService; @RequestMapping(method = RequestMethod.GET) public ModelAndView showUserLogin(Map model, HttpSession session) { if (session.getAttribute("userEmail") == null) { User user = new User(); model.put("user", user); return new ModelAndView("/user/userlogin"); } else { return new ModelAndView("redirect:selectpaper.html"); } } @RequestMapping(method = RequestMethod.POST) public ModelAndView processUserLogin(@Valid User user, BindingResult result, Map model, HttpSession session) { userLoginValidator.validate(user, result); if (result.hasErrors()) { return new ModelAndView("/user/userlogin"); } List userlist = userService.getUserByUserEmail(user.getUserEmail()); if (userlist != null && userlist.size() > 0) { User user1 = new User(); user1 = (User) userlist.get(0); session.setAttribute("userName", user1.getUserName()); } session.setAttribute("userEmail", user.getUserEmail()); return new ModelAndView("redirect:selectpaper.html"); } }
UserLogOutController.java
This controller class handles the logout of the user.
package net.devmanuals.controller; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/user/logout") public class UserLogOutController { @RequestMapping(method = RequestMethod.GET) public ModelAndView logOut(HttpSession session) { session.invalidate(); return new ModelAndView("redirect:userlogin.html"); } }
UserRegistrationController.java
This controller class handles the user registration process.
package net.devmanuals.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.validation.BindingResult; import java.util.Map; import javax.validation.Valid; import net.devmanuals.model.User; import net.devmanuals.service.UserService; import net.devmanuals.utils.Encryption; import net.devmanuals.validators.UserForm; import net.devmanuals.validators.UserRegistrationValidator; @Controller @RequestMapping("/user/userregistration") public class UserRegistrationController { @Autowired private UserService userService; @Autowired UserRegistrationValidator userRegistrationValidator; @RequestMapping(method = RequestMethod.GET) public String showUserLogin(Map model) { UserForm userForm = new UserForm(); model.put("userForm", userForm); return "/user/userregistration"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView processUserLogin(@Valid UserForm userForm, BindingResult result, Map model) { userRegistrationValidator.validate(userForm, result); if (result.hasErrors()) { return new ModelAndView("/user/userregistration"); } else { // set userForm value to user object. User user = new User(); user.setUserName(userForm.getUserName()); user.setUserEmail(userForm.getUserEmail()); user.setPassword(Encryption.encrypt(userForm.getPassword())); user.setUserGender(userForm.getUserGender()); user.setPhoneNo(userForm.getPhoneNo()); user.setAddress(userForm.getAddress()); userService.saveUser(user); } return new ModelAndView("redirect:userlogin.html"); } }
[ 0 ] Comments