쌍용교육(JAVA)/MVC

쌍용교육 -JSP수업 55일차 ch04_mvcBoard - MVC(3)

구 승 2024. 5. 8. 12:31

MVC를 제대로 하기위한 기본 세팅 및 연결 확인 

 

ch04_mvcBoard 생성 

 

webapp => sql 폴더생성 =>

table.sql

--MVC 게시판
create table svboard(
num number primary key,
title varchar2(150) not null,
name varchar2(30) not null,
passwd varchar2(12) not null,
email varchar2(50) not null,
content clob not null,
ip varchar2(30) not null,
reg_date date default sysdate not null
);
create sequence svboard_seq;

src/main/java =>

kr.controller 패키지 생성 =>

기존에 있던 Action.java 붙여넣기

 

package kr.controller;

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

public interface Action { //인터페이스를 만들지않고 class를 하고싶다면 추상클래스로 만들어서 사용하면된다.
	//추상메서드
	public String execute (HttpServletRequest request, HttpServletResponse response) throws Exception;
}

파일추가

DispatcherServlet.java
0.00MB

kr.controller의 넣기

 

 

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> ch04_mvcBoard</display-name>
  <servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>kr.controller.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
</web-app>

webapp =>

index.jsp 

아무거나 입력 후 실행(ex:body에 test입력)  Redirect를 하기위해

Fimish 클릭
이 화면이 나온다면 Redirect 성공

index.jsp

위에서 썼던 body안에 test를 지우면서 HTML내용을 모두 지운 후 작성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	response.sendRedirect(request.getContextPath()+"/list.do");
%>

kr.board.action =>

ListAction

package kr.board.action;

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

import kr.controller.Action;

public class ListAction implements Action{

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//request
		//request.setAttribute("", "");
		//JSP경로반환
		return "/WEB-INF/views/list.jsp";
	}

}

WEB-INF =>

   views =>

     list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>목록</title>
</head>
<body>
목록 페이지입니다.
</body>
</html>

WEB-INF =>

ActionMap.properties 파일로 생성 

(servlet을 계속 수정하면 좋지않기 때문에 이 파일을 이용함) 

한글을 쓰면 유니코드로 자동변경 되기 때문에 플러그인을 사용하여 바꾼다.

 

 

properties 입력 후 검색 및 Kantan에디터를 install

마지막으로 Restart를 하고나면 끝.

ActionMap.properties

 

파일 작성을 하기위해선 이 작업을 해야됨

ActionMap.properties (Kantan 으로 작성)

#DispatcherServlet에서 읽어갈 정보를
#key와 value의 쌍으로 명시

이 방식으로 보면 위에서 작성한 것이 유니코드로 변경되어있는 것을 볼 수 있음.

ActionMap.properties(text editor로 본것)

#DispatcherServlet\uC5D0\uC11C \uC77D\uC5B4\uAC08 \uC815\uBCF4\uB97C
#key\uC640 value\uC758 \uC30D\uC73C\uB85C \uBA85\uC2DC

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> ch04_mvcBoard</display-name>

<servlet>

<servlet-name>DispatcherServlet</servlet-name>

<servlet-class>kr.controller.DispatcherServlet</servlet-class>

<init-param>

<param-name>propertiesPath</param-name>

<param-value>/WEB-INF/ActionMap.properties</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>DispatcherServlet</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.jsp</welcome-file>

<welcome-file>default.htm</welcome-file>

</welcome-file-list>

</web-app>

 

추가한 부분

<init-param>

<param-name>propertiesPath</param-name>

<param-value>/WEB-INF/ActionMap.properties</param-value>

</init-param>

 

ActionMap.properties

#DispatcherServlet에서 읽어갈 정보를
#key와 value의 쌍으로 명시
#요청 URL = 모델클래스
/list.do = kr.board.action.ListAction

kr.board.action.ListAction 뒤에 공백이 있으면 에러가 난다.

 

index.jsp 실행화면

webapp => css 폴더 =>

   ch03_JSP에 있던 css=> style.css 파일 copy하기

 

@charset "UTF-8";

body{
	padding:0;
	margin:0;
}
.page-main{
	width:800px;
	margin:0 auto; /* 중양 정렬 */
}
.result-display{
	width:400px;
	height:200px;
	margin:50px;
	margin:50px auto;
	border:1px solid #000;
	display:flex;
	align-items:center; /* 세로 정렬 */
	justify-content:center;/* 가로 정렬 */
}
.align-center{
	text-align:center;
}
.align-right{
	text-align:right;
}
/* 목록 */
table{
	width:100%;
	border:1px solid #000;
	border-collapse: collapse;
	margin-top:5px;
}
table td, table th{
	border:1px solid #000;
	padding:5px;
}
/* 등록,수정폼*/
form{
	width:500px;
	margin:0 auto;
	border:1px solid #000;
	padding:10px 10px 10px 30px;
}
ul{
	list-style:none;
}
label{
	width:100px;
	float:left; /* 태그를 왼쪽으로 정렬 */
}