Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6주차 미션 / 서버 1조 김지현 #63

Open
wants to merge 2 commits into
base: step5-mvc-modelAndView
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/core/mvc/AbstractController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.mvc;

import core.mvc.view.JsonView;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;


public abstract class AbstractController implements Controller {

protected ModelAndView jspView(String viewName){
return new ModelAndView(new JspView(viewName));
}

protected ModelAndView jsonView(){
return new ModelAndView(new JsonView());
}

}
3 changes: 2 additions & 1 deletion src/main/java/core/mvc/Controller.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package core.mvc;

import core.mvc.view.ModelAndView;
import core.mvc.view.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Controller {
View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception;
ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception;
}
8 changes: 3 additions & 5 deletions src/main/java/core/mvc/DispatcherServlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package core.mvc;

import core.mvc.view.ModelAndView;
import core.mvc.view.View;

import javax.servlet.RequestDispatcher;
Expand All @@ -25,11 +26,8 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
String url = req.getRequestURI();
Controller controller = requestMapping.getController(url);
try {
View view = controller.execute(req, resp);
if (view == null) {
return;
}
view.render(req, resp);
ModelAndView mav = controller.execute(req, resp);
mav.render(req, resp);
} catch (Exception e) {
System.out.println(e.getMessage());
throw new ServletException(e.getMessage());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/mvc/view/JsonView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class JsonView implements View{
@Override
public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {
public void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/core/mvc/view/JspView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class JspView implements View {

Expand All @@ -15,7 +16,7 @@ public JspView(String viewName) {
}

@Override
public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {
public void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (viewName.startsWith(REDIRECT_PREFIX)) {
response.sendRedirect(viewName.substring(REDIRECT_PREFIX.length()));
return;
Expand All @@ -24,3 +25,5 @@ public void render(HttpServletRequest request, HttpServletResponse response) thr
rd.forward(request, response);
}
}


33 changes: 33 additions & 0 deletions src/main/java/core/mvc/view/ModelAndView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.mvc.view;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class ModelAndView {

View view;
Map<String, Object> model = new HashMap<>();

public ModelAndView(View view) {
this.view = view;
}

public static ModelAndView jspView(String viewName){
return new ModelAndView(new JspView(viewName));
}

public static ModelAndView jsonView(){
return new ModelAndView(new JsonView());
}

public ModelAndView addObject(String attributeName, Object attributeValue){
model.put(attributeName, attributeValue);
return this;
}

public void render(HttpServletRequest req, HttpServletResponse resp) throws Exception {
view.render(model, req, resp);
}
}
3 changes: 2 additions & 1 deletion src/main/java/core/mvc/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public interface View {
void render(HttpServletRequest request, HttpServletResponse response) throws Exception;
void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
11 changes: 7 additions & 4 deletions src/main/java/jwp/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package jwp.controller;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.QuestionDao;
import jwp.model.Question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class HomeController implements Controller {
public class HomeController extends AbstractController {

private final QuestionDao questionDao = new QuestionDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<Question> questions = questionDao.findAll();
req.setAttribute("questions", questions);
return new JspView("/home.jsp");
return jspView("/home.jsp").addObject("questions", questions);
}

}
8 changes: 5 additions & 3 deletions src/main/java/jwp/controller/qna/CreateAnswerController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JsonView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.AnswerDao;
import jwp.dao.QuestionDao;
Expand All @@ -11,13 +13,13 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateAnswerController implements Controller {
public class CreateAnswerController extends AbstractController {

private final AnswerDao answerDao = new AnswerDao();
private final QuestionDao questionDao = new QuestionDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("[CreateAnswerController] 실행");
int questionId = Integer.parseInt(req.getParameter("questionId"));
Answer answer = new Answer(questionId,
Expand All @@ -32,6 +34,6 @@ public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exc

req.setAttribute("answer", savedAnswer);

return new JsonView();
return jsonView();
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.QuestionDao;
import jwp.model.Question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateQuestionController implements Controller {
public class CreateQuestionController extends AbstractController {

private final QuestionDao questionDao = new QuestionDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Question question = new Question(
req.getParameter("writer"),
req.getParameter("title"),
req.getParameter("contents"),
0);
Question savedQuestion = questionDao.insert(question);
System.out.println("saved question id= " + savedQuestion.getQuestionId());
return new JspView("redirect:/");
return jspView("redirect:/");
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.util.UserSessionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CreateQuestionFormController implements Controller {
public class CreateQuestionFormController extends AbstractController {

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
HttpSession session = req.getSession();
if (UserSessionUtils.isLogined(session)) { // 회원만 질문 등록 가능
return new JspView("/qna/form.jsp");
return jspView("/qna/form.jsp");
}
return new JspView("redirect:/user/loginForm");
return jspView("redirect:/user/loginForm");
}
}
8 changes: 5 additions & 3 deletions src/main/java/jwp/controller/qna/ShowQnaController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.AnswerDao;
import jwp.dao.QuestionDao;
Expand All @@ -12,18 +14,18 @@
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ShowQnaController implements Controller {
public class ShowQnaController extends AbstractController {

private final QuestionDao questionDao = new QuestionDao();
private final AnswerDao answerDao = new AnswerDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String questionId = req.getParameter("questionId");
Question question = questionDao.findByQuestionId(Integer.parseInt(questionId));
List<Answer> answers = answerDao.findAllByQuestionId(question.getQuestionId());
req.setAttribute("question", question);
req.setAttribute("answers", answers);
return new JspView("/qna/show.jsp");
return jspView("/qna/show.jsp");
}
}
10 changes: 6 additions & 4 deletions src/main/java/jwp/controller/qna/UpdateQuestionController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.QuestionDao;
import jwp.model.Question;
Expand All @@ -12,15 +14,15 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UpdateQuestionController implements Controller {
public class UpdateQuestionController extends AbstractController {

private final QuestionDao questionDao = new QuestionDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
HttpSession session = req.getSession();
if (!UserSessionUtils.isLogined(session)) {
return new JspView("redirect:/users/loginForm");
return jspView("redirect:/users/loginForm");
}

User user = UserSessionUtils.getUserFromSession(session);
Expand All @@ -32,6 +34,6 @@ public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exc
question.updateTitleAndContents(req.getParameter("title"),
req.getParameter("contents"));
questionDao.update(question);
return new JspView("redirect:/");
return jspView("redirect:/");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jwp.controller.qna;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.QuestionDao;
import jwp.model.Question;
Expand All @@ -13,15 +15,15 @@
import javax.servlet.http.HttpSession;


public class UpdateQuestionFormController implements Controller {
public class UpdateQuestionFormController extends AbstractController {

private final QuestionDao questionDao = new QuestionDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
HttpSession session = req.getSession();
if (!UserSessionUtils.isLogined(session)) { // 회원만 질문 등록 가능
return new JspView("redirect:/user/loginForm");
return jspView("redirect:/user/loginForm");
}
String questionId = req.getParameter("questionId");
Question question = questionDao.findByQuestionId(Integer.parseInt(questionId));
Expand All @@ -30,7 +32,7 @@ public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exc
throw new IllegalArgumentException();
}
req.setAttribute("question", question);
return new JspView("/qna/updateForm.jsp");
return jspView("/qna/updateForm.jsp");
}

}
8 changes: 5 additions & 3 deletions src/main/java/jwp/controller/user/CreateUserController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package jwp.controller.user;

import core.mvc.AbstractController;
import core.mvc.Controller;
import core.mvc.view.JspView;
import core.mvc.view.ModelAndView;
import core.mvc.view.View;
import jwp.dao.UserDao;
import jwp.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateUserController implements Controller {
public class CreateUserController extends AbstractController {

private final UserDao userDao = new UserDao();

@Override
public View execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
public ModelAndView execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = new User(req.getParameter("userId"),
req.getParameter("password"),
req.getParameter("name"),
req.getParameter("email"));
userDao.insert(user);
System.out.println("user 회원가입 완료");
return new JspView("redirect:/user/list");
return jspView("redirect:/user/list");
}
}
Loading