In this Sample projects you will find a web application of forum, where users can leave their comments and discuss on their question
Java Forum
The present web application is a online discussion application, in which user can discuss on their particular topics. The user log in or register and reads the questions and comments and can also post their questions or comments. This application uses the following web technologies.
- Spring - As a web technology.
- Hibernate - for handling database connections.
- JPA - for maintaining persistence data in the application.
- Eclipse - for compilation and building the application.
- Tomcat 6 - for running application
Its configuration is similar to the previous project but it is build in Eclipse rather than Maven. The following is the Controller class.
ForumControler.java
package roseindia.forum.application.controller; import java.sql.Timestamp; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import roseindia.forum.application.domain.Answer; import roseindia.forum.application.domain.User; import roseindia.forum.application.form.LoginForm; import roseindia.forum.application.form.QuestionForm; import roseindia.forum.application.form.RegistrationForm; import roseindia.forum.application.service.ForumService; @Controller @RequestMapping(value = "/forum") public class ForumControler { @Resource(name = "forumService") private ForumService forumService; @RequestMapping(value = "/login-page") public String loadForumPage(Map model, LoginForm loginForm) { model.put("loginForm", loginForm); return "login-page"; } @RequestMapping(value = "/process-login") public String processLogin(Map model, LoginForm loginForm, HttpSession session) { if (loginForm != null) { User user = forumService.loginUser(loginForm); if (user != null) { session.setAttribute("userName", user.getUserName()); session.setAttribute("user", user); return "forward:user-home"; } } model.put("loginForm", loginForm); return "login-page"; } @RequestMapping(value = "/sign-up") public String loadSignUp(Map model, RegistrationForm registrationForm) { model.put("registrationForm", registrationForm); return "sign-up"; } @RequestMapping(value = "/process-sign-up") public String processSignUp(Map model, RegistrationForm registrationForm, HttpSession session) { try { forumService.signUpUser(registrationForm); LoginForm loginForm = new LoginForm(); loginForm.setUserId(registrationForm.getUserId()); loginForm.setPassword(registrationForm.getPassword()); /*User user = forumService.swap(registrationForm); session.setAttribute("user", user); session.setAttribute("userName", registrationForm.getUserName());*/ return "forward:process-login"; } catch (Exception exception) { exception.printStackTrace(); model.put("registrationForm", registrationForm); return "sign-up"; } } @RequestMapping(value = "/user-home") public String userHome(Map model, HttpSession session) { if (session.getAttribute("userName") != null) { model.put("questionList", forumService.getAllQuestions()); return "home-page"; } return "login-page"; } @RequestMapping(value = "/add-comment") public String addComment(Map model, HttpSession session, HttpServletRequest request) { int questionId = 0; String comment = ""; User user = new User(); if (session.getAttribute("userName") != null) { user = (User) session.getAttribute("user"); if (request.getParameter("questionId") != null) { questionId = Integer.parseInt((String) request .getParameter("questionId")); } if (request.getParameter("comment") != null) { comment = (String) request.getParameter("comment"); if (comment == "") { return "forward:user-home"; } } Answer answer = new Answer(); answer.setAnswer(comment); answer.setQuestionId(questionId); answer.setUserId(user.getId()); answer.setDate(new Timestamp(System.currentTimeMillis())); forumService.addAnswers(answer); model.put("questionList", forumService.getAllQuestions()); comment = ""; questionId = 0; return "forward:user-home"; } return "login-page"; } @RequestMapping(value = "/post-question") public String postQuestion(Map model, HttpSession session, QuestionForm questionForm) { if (session.getAttribute("user") != null && session.getAttribute("userName") != null) { User user = (User) session.getAttribute("user"); questionForm.setUserId(user.getId()); model.put("questionForm", questionForm); return "question-post"; } return "forward:login-page"; } @RequestMapping(value = "/add-question") public String addQuestion(Map model, HttpSession session, QuestionForm questionForm) { if (session.getAttribute("user") != null && session.getAttribute("userName") != null) { if (!questionForm.getQuestion().equals("")) { forumService.addQuestion(questionForm); } return "forward:user-home"; } return "forward:login-page"; } @RequestMapping(value = "/logout") public String logout(Map model, HttpSession session) { if (session.getAttribute("user") != null && session.getAttribute("userName") != null) { session.removeAttribute("user"); session.removeAttribute("userName"); session.invalidate(); } return "forward:login-page"; } }
Download the complete source code with database.
When you will run the above project you will see the following output
[ 0 ] Comments