기능 개선
매번 ModelView 객체를 생성하지 않고, 컨트롤러가 ModelView를 반환하지 않고 ViewName만 반환한다.
인터페이스에는 ModelView가 없으며, model 객체를 파라미터로 전달하기 때문에 그냥 사용하면 된다. 결과적으로 viewName만 반환해주면 된다.
public interface ControllerV4 {
/**
*
* @param paramMap
* @param model
* @return viewName
*/
String process(Map<String, String> paramMap, Map<String, Object> model);
}
ControllerV4 인터페이스를 준수하는 컨트롤러도 마찬가지로 viewName만 반환하도록 설정한다. 대신 파라미터로 전달되는 model에 비즈니스 로직을 반영하여야 한다.
public class MemberFormControllerV4 implements ControllerV4 {
@Override
public String process(Map<String, String> paramMap, Map<String, Object>
model) {
return "new-form";
}
}
public class MemberListControllerV4 implements ControllerV4 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public String process(Map<String, String> paramMap, Map<String, Object>
model) {
List<Member> members = memberRepository.findAll();
model.put("members", members);
return "members";
}
}
public class MemberSaveControllerV4 implements ControllerV4 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public String process(Map<String, String> paramMap, Map<String, Object> model) {
String username = paramMap.get("username");
int age = Integer.parseInt(paramMap.get("age"));
Member member = new Member(username, age);
memberRepository.save(member);
model.put("member", member);
return "save-result";
}
}
FrontController도 model을 정의하고, 컨트롤러에 전달하는 것이 추가된다.
@WebServlet(name = "frontControllerServletV4", urlPatterns = "/front-controller/v4/*")
public class FrontControllerServletV4 extends HttpServlet {
private Map<String, ControllerV4> controllerMap = new HashMap<>();
public FrontControllerServletV4() {
controllerMap.put("/front-controller/v4/members/new-form", new MemberFormControllerV4());
controllerMap.put("/front-controller/v4/members/save", new MemberSaveControllerV4());
controllerMap.put("/front-controller/v4/members", new MemberListControllerV4());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
ControllerV4 controller = controllerMap.get(requestURI);
if (controller == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
Map<String, String> paramMap = createParamMap(request);
// model 정의
Map<String, Object> model = new HashMap<>();
String viewName = controller.process(paramMap, model);
MyView view = viewResolver(viewName);
view.render(model, request, response);
}
private Map<String, String> createParamMap(HttpServletRequest request) {
Map<String, String> paramMap = new HashMap<>();
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> paramMap.put(paramName,
request.getParameter(paramName)));
return paramMap;
}
private MyView viewResolver(String viewName) {
return new MyView("/WEB-INF/views/" + viewName + ".jsp");
}
}
'🌱 Spring > MVC ①' 카테고리의 다른 글
[Spring] MVC _ @RequestMapping (1) (0) | 2023.07.22 |
---|---|
[Spring] MVC _ 어댑터 패턴 (0) | 2023.07.19 |
[Spring] MVC _ ViewModel 추가 (0) | 2023.07.17 |
[Spring] MVC _ Front Controller 도입, View 분리 (0) | 2023.07.17 |
[Spring] 회원 관리 웹 어플리케이션 4 _ ServletMVC 패턴 (0) | 2023.07.15 |