웹 프로그래밍/Web
[Web] 3. MVC 패턴(2) - Main과 Param 동작방식
코딩하는 문과생
2020. 1. 30. 11:49
[Preview]

[tomcat은 index.jsp를 먼저 찾는다.]
- webapp아래 index.jsp 생성
- http://127.0.0.1:8088/incWEB/ 접속
- Tomcat은 index.jsp를 먼저 찾는다.

[index.jsp 수정]
루트로 접근하면 main.inc로 리다이렉트 된다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<script type="text/javascript">
location.href = "main.inc";
</script>
[main ctrl 생성]
package com.sinc.intern.main.ctrl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sinc.intern.util.Controller;
import com.sinc.intern.view.util.ModelAndView;
public class MainCtrl implements Controller{
@Override
public ModelAndView execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("MainCtrl execute");
return new ModelAndView(true, "main.jsp");
}
}
[main.jsp]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align = "center">웹 프레임워크 by ㅋㅋㅋ</div>
<hr/>
Post :: <br/>
<form action="parameter.inc" method="post">
<input type="text" name="msg" id="msg">
<input type="submit" value="이벤트">
</form>
Get :: <br/>
<a href = "parameter.inc?msg=sijune">링크</a>
<!-- a태그는 무조건 get방식 -->
</body>
</html>

[BeanFactory로 MainCtrl생성 ]
private BeanFactory() {
map.put("/incWEB/main.inc", new MainCtrl());
map.put("/incWEB/insert.inc", new InsertCtrl());
map.put("/incWEB/select.inc", new SelectCtrl());
}

- Get방식 & Post방식 확인하기
[ParamCtrl 생성]
public class ParamCtrl implements Controller{
@Override
public ModelAndView execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
return null;
}
}
[BeanFactory 추가]
private BeanFactory() {
map.put("/incWEB/main.inc", new MainCtrl());
map.put("/incWEB/parameter.inc", new ParamCtrl());
map.put("/incWEB/insert.inc", new InsertCtrl());
map.put("/incWEB/select.inc", new SelectCtrl());
}
[ParamCtrl 수정]
request 요청정보 확인
public class ParamCtrl implements Controller{
@Override
public ModelAndView execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//request에 요청정보가 들어온다.
System.out.println("method : " + request.getMethod());
//유효성 체크도 가능하지만, 보통 front에서 처리한다.
request.setCharacterEncoding("utf-8");
String msg = request.getParameter("msg");
System.out.println("Param result : " + msg);
return new ModelAndView(true, "greeting.jsp");
}
}



