2019. 8. 19. 22:50ㆍJSP
#1 MVC 구조
#2 ActionForward 클래스를 vo패키지에 추가한다.
package vo;
public class ActionForward { //1번 private String path; // 경로정보 private boolean redirect; // true:리다이렉트, false:디스패치
public ActionForward() { }
public ActionForward(String path, boolean redirect) { super(); this.path = path; this.redirect = redirect; }
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
public boolean isRedirect() { return redirect; }
public void setRedirect(boolean redirect) { this.redirect = redirect; } } |
#3 action 패키지에 Action이라는 interface를 추가한다.
package action;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import vo.ActionForward;
public interface Action { //2번 ActionForward execute(HttpServletRequest req, HttpServletResponse resp) throws Exception; }
|
#4 action패키지에 homeAction, 즉 첫 화면을 나타내는 Action을 추가한다.
package action;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import vo.ActionForward;
public class HomeAction implements Action{
//3번 @Override public ActionForward execute(HttpServletRequest req,HttpServletResponse resp)throws Exception { ActionForward forward = new ActionForward(); forward.setPath("home"); forward.setRedirect(false); return forward; } }
|
#4 action패키지에 ActionFactory를 추가한다.
여기서 ActionFactory는 명령어에 따라 action을 담당하는 공장이 된다.
package action;
import java.util.HashMap; import java.util.Map;
public class ActionFactory { //4번 private static ActionFactory factory = new ActionFactory();
public static ActionFactory getInstance() { return factory; }
private Map<String,Action> map;
private ActionFactory() { map = new HashMap<>(); map.put("home", new HomeAction()); }
public Action getAction(String command) { return map.get(command); } } |
#5 FrontController , controller 패키지에 추가한다.
화면에 대한 controll을 담당하는 controller이다.
package controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import action.Action; import action.ActionFactory; import vo.ActionForward;
@WebServlet("*.do") public class FrontController extends HttpServlet{ // 5번 private static final long serialVersionUID = 1L;;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 명령Command 확인 및 주소만들기 String requestURI = req.getRequestURI();
String contextPath = req.getContextPath(); String command = requestURI.substring(contextPath.length());
int index = command.lastIndexOf("."); command = command.substring(1,index);
System.out.println("URI주소: " + requestURI); System.out.println("contextPath: " + contextPath); System.out.println("command: " + command); System.out.println("명령어command: " + command); // 주소로 이동 Action action = null; ActionForward forward = null; ActionFactory factory = ActionFactory.getInstance();
action = factory.getAction(command); if(action != null) { try { forward = action.execute(req, resp); } catch (Exception e) { e.printStackTrace(); } }//if
if(forward != null) { if(forward.isRedirect()) { resp.sendRedirect(forward.getPath()); }else { String path ="WEB-INF/views/"+forward.getPath()+".jsp"; RequestDispatcher dispathcher = req.getRequestDispatcher(path); dispathcher.forward(req, resp); } }//if
}
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");//한글처리 doGet(req,resp); }
} |
#6 index.jsp
첫 화면의 리다이렉트를 담당한다.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:redirect url="home.do" /> |
#7 web.xml
마찬가지로 첫 화면을 띄우기 위해 설정한다.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>test</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> </web-app> |
'JSP' 카테고리의 다른 글
2019.02.20 Model2 : 게시판 만들기(6) (0) | 2019.02.20 |
---|---|
2019.02.20 Model2 : 게시판 만들기(4) (0) | 2019.02.20 |
2019.02.19 Model2 : 게시판 만들기(3) (0) | 2019.02.19 |
2019.02.19 Model2 : 게시판 만들기(2) (0) | 2019.02.19 |
2019.02.18 Model2 : 게시판 만들기(1) (0) | 2019.02.18 |