모델1방식... view와 model만 있는 방식으로 하는건 좀.. 없어보여서..
도서관에서 jsp2.1 mvc패턴 활용까지 어쩌구하는 책을 빌려서 공부해보았다.
삽질을 조금했지만 그래도 순탄하다.
View : 화면 출력
Model : 데이터베이스 연동하는 부분 로직
Controller : 뷰와 모델 연결
이클립스 개발환경은 다음과 같다.
딱봐도 감이 오지 않는가~!
Action과 ActionForward 클래스엔 별거 없다. 괜히 쫄았넹..
FrontController클래스가 핵심이다. 포워딩을 정의하고 있다. 요청이 들어오면 어디로 보내줘야 하는지...
FrontController.java
public class FrontController extends HttpServlet implements Servlet {
protected void doProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
String RequestURI=request.getRequestURI();
String contextPath=request.getContextPath();
String command=RequestURI.substring(contextPath.length());
ActionForward forward=null;
Action action=null;
if(command.equals("/BoardWrite.bo")){
try{
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("./board/board_write.jsp");
System.out.println("글쓰기폼");
}catch (Exception e) {
// TODO: handle exception
System.out.println("에라다다"+e);
}
}if(forward != null){ //실질적으로 페이지를 이동시켜주는 로직
if(forward.isRedirect()){
response.sendRedirect(forward.getPath());
}else{ //실질적으로 페이지를 이동시켜주는 로직
RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
try {
dispatcher.forward(request, response);
System.out.println("엘스로"); //톰캣로그를 확인하기위해.. 새로발견
} catch (Exception e) {
System.out.println("에라다다"+e);
}
}
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
doProcess(req, res);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
doProcess(req, res);
}
}
public class FrontController extends HttpServlet implements Servlet {
protected void doProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
String RequestURI=request.getRequestURI();
String contextPath=request.getContextPath();
String command=RequestURI.substring(contextPath.length());
ActionForward forward=null;
Action action=null;
if(command.equals("/BoardWrite.bo")){
try{
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("./board/board_write.jsp");
System.out.println("글쓰기폼");
}catch (Exception e) {
// TODO: handle exception
System.out.println("에라다다"+e);
}
}if(forward != null){ //실질적으로 페이지를 이동시켜주는 로직
if(forward.isRedirect()){
response.sendRedirect(forward.getPath());
}else{ //실질적으로 페이지를 이동시켜주는 로직
RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
try {
dispatcher.forward(request, response);
System.out.println("엘스로"); //톰캣로그를 확인하기위해.. 새로발견
} catch (Exception e) {
System.out.println("에라다다"+e);
}
}
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
doProcess(req, res);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
doProcess(req, res);
}
}
web.xml 에 다음 부분을 추가시켜주자.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>BoardFrontController</servlet-name>
<servlet-class>
net.board.action.BoardFrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BoardFrontController</servlet-name>
<url-pattern>*.bo</url-pattern>
</servlet-mapping>
<display-name>
Model2-Board</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Connection</description>
<res-ref-name>jdbc/OracleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
그렇게하면 http://localhost/mvc/BoardWrite.bo 로 들어가면 글쓰기폼이 뜬다는 것!!
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>BoardFrontController</servlet-name>
<servlet-class>
net.board.action.BoardFrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BoardFrontController</servlet-name>
<url-pattern>*.bo</url-pattern>
</servlet-mapping>
<display-name>
Model2-Board</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Connection</description>
<res-ref-name>jdbc/OracleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
그렇게하면 http://localhost/mvc/BoardWrite.bo 로 들어가면 글쓰기폼이 뜬다는 것!!
삽질 좀 했지만, 결국 톰캣서버가 리로딩될 때까지 기다리고.. 리로딩된 후 새로고침이 중요하단걸 ... 이거 뭐 엉뚱한데서 답을 찾았어!!