FullStack Study/10주차

JAVA 풀스택 44일차 - Server (JSP MVC)

레몬팡777 2026. 5. 29. 08:44

JSP 게시판 MVC 기능 구현

상세 조회 / 글쓰기 / 등록 / 수정 / 삭제

1. command 값으로 기능 구분

Controller가 어떤 기능을 실행할지 결정한다.

command=boardlist        → 목록 조회
command=boarddetail      → 상세 조회
command=boardinsertform  → 글쓰기 화면 이동
command=boardinsert      → 게시글 등록
command=boardupdateform  → 수정 화면 이동
command=boardupdate      → 게시글 수정
command=boarddelete      → 게시글 삭제

 

if(command.equals("boarddetail")) {
    ...
} else if(command.equals("boardinsertform")) {
    ...
} else if(command.equals("boardinsert")) {
    ...
}

- command는 Controller에게 전달하는 기능 이름이다

2. 상세 조회 기능

게시글 목록에서 제목을 클릭하면 상세보기로 이동

<a href="mycontroller.jsp?command=boarddetail&seq=<%=dto.getSeq()%>">
    <%=dto.getTitle() %>
</a>
mycontroller.jsp?command=boarddetail&seq=글번호

 

부분 의미
mycontroller.jsp Controller JSP로 요청
command=boarddetail 상세 조회 기능 실행 요청
seq=글번호 몇 번 글인지 전달

2-1 파라미터가 2개일 때

URL 파라미터가 여러 개일 때는 &로 연결

mycontroller.jsp?command=boarddetail&seq=3

 

구분 의미
?command=boarddetail 첫 번째 파라미터
&seq=3 두 번째 파라미터

?는 파라미터 시작, &는 파라미터 추가

3. boarddetail.jsp에서 상세 내용 출력

boarddetail.jsp는 Controller가 넘긴 dto를 꺼낸다.

MVCBoardDto dto = (MVCBoardDto)request.getAttribute("dto");
<%=dto.getSeq() %>
<%=dto.getWriter() %>
<%=dto.getTitle() %>
<%=dto.getContent() %>

- JSP 표현식으로 화면에 출력

boardlist.jsp
↓
제목 클릭
↓
mycontroller.jsp?command=boarddetail&seq=글번호
↓
dao.selectOne(seq)
↓
request.setAttribute("dto", res)
↓
boarddetail.jsp
↓
상세 내용 출력

4. 글쓰기 화면 이동 기능

목록 페이지에서 글쓰기 버튼을 누르면 Controller로 이동한다

<input type="button" value="글쓰기"
       onclick="location.href='mycontroller.jsp?command=boardinsertform'">

- Controller에서는 boardinsertform 요청을 받으면 글쓰기 페이지로 이동

response.sendRedirect("boardinsert.jsp");

- 글쓰기 화면으로 단순 이동만 하면 되기에 redirect 사용

조회한 데이터를 request에 담아 보낼 필요 없음
→ redirect 사용 가능

5. boardinsert.jsp

5-1 글쓰기 form

boardinsert.jsp는 사용자가 글을 작성하는 화면

<form action="mycontroller.jsp" method="post">

- 입력 값을 mycontroller.jsp로 보낸다

5-2 hidden으로 command 전달

<input type="hidden" name="command" value="boardinsert">

숨겨진 input을 통해 command 값을 Controller로 전달한다.

 

5-3 입력값 name 속성

사용자가 입력하는 값들은 각각 name을 가진다

<input type="text" name="writer">
<input type="text" name="title">
<textarea rows="10" cols="60" name="content"></textarea>

Controller에서 값을 꺼낼 때 name 값을 사용

request.getParameter("writer");
request.getParameter("title");
request.getParameter("content");

6. 게시글 등록 처리

6-1 Controller에서 form 값 받기

등록 버튼을 누르면 form 데이터가 mycontroller.jsp로 전송

Controller에서는 command=boardinsert를 확인하고 글 등록을 처리

String writer = request.getParameter("writer");
String title = request.getParameter("title");
String content = request.getParameter("content");

- form에서 입력한 작성자, 제목, 내용을 Controller에서 받는다.

6-2 DTO 객체 생성

받은 값을 DTO 객체에 담는다.

MVCBoardDto dto = new MVCBoardDto(0, writer, title, content, null);

6-3 DAO insert 실행

Controller는 DTO를 DAO로 전달한다.

int res = dao.insert(dto);

- DAO에서 INSERT 작업을 실행하고, 결과를 res로 돌려준다

res > 0 → 등록 성공
res == 0 → 등록 실패

7. result.jsp 공통 결과 페이지

등록, 수정, 삭제 결과는 result.jsp에서 공통으로 출력한다.

msg = 글 작성 완료
url = ?command=boardlist
request.setAttribute("msg", msg);
request.setAttribute("url", url);
pageContext.forward("result.jsp");

- Controller에서는 메시지와 이동할 주소를 request에 저장한다.

String msg = (String)request.getAttribute("msg");
String url = (String)request.getAttribute("url");
- result.jsp에서는 이 값을 꺼낸다
<button onclick="location.href='mycontroller.jsp<%=url%>'">확인</button>

- 메시지를 출력하고, 확인 버튼을 누르면 다시 Controller로 이동한다.

의미
msg 화면에 출력할 메시지
url 확인 버튼 클릭 시 이동할 주소

8. 게시글 수정 화면

8-1 boardupdate.jsp

boardupdate.jsp는 게시글 수정 화면

수정 화면은 빈 입력창을 보여주는 것이 아니라, 기존 게시글 내용을 먼저 가져와서 form 안에 출력한다.

Controller에서 기존 게시글 조회
↓
request.setAttribute("dto", dto)
↓
boardupdate.jsp로 forward
↓
boardupdate.jsp에서 dto 꺼내기
↓
form에 기존 값 출력

8-2 기존 값 출력

수정 화면에서는 form 안에 기존 내용을 넣어준다

<input type="text" name="title" value="<%=dto.getTitle() %>">

- 기존 제목을 input 태그 안에 출력한다

9. hidden input

<input type="hidden" name="command" value="boardupdate">
<input type="hidden" name="seq" value="<%=dto.getSeq()%>">

hidden은 화면에는 보이지 않지만, submit할 때 함께 전송되는 값이다

hidden 값 의미
command=boardupdate Controller에게 수정 기능 실행 요청
seq=게시글번호 몇 번 글을 수정할지 전달

10. 게시글 수정 처리

수정 버튼을 누르면 form 데이터가 Controller로 전송된다.

command = boardupdate
seq = 게시글 번호
title = 수정한 제목
content = 수정한 내용

- 전송되는 값

command=boardupdate 확인
↓
seq, title, content 값 받기
↓
DTO 객체 생성
↓
dao.update(dto)
↓
결과에 따라 msg, url 저장
↓
result.jsp로 forward

 

- Controller에서는 boardupdate 요청을 확인한 뒤 값을 받는다.

MVCBoardDto dto = new MVCBoardDto(seq, null, title, content, null);

- 작성자와 작성일은 수정하지 않으므로 null로 둔다

String sql = " UPDATE MVCBOARD SET TITLE=?, CONTENT=? WHERE SEQ=? ";
의미
seq 수정할 게시글 번호
title 수정한 제목
content 수정한 내용

11. 게시글 삭제 처리

흐름

삭제 버튼 클릭
↓
mycontroller.jsp?command=boarddelete&seq=게시글번호
↓
Controller에서 seq 받기
↓
dao.delete(seq)
↓
결과에 따라 result.jsp로 이동
int seq = Integer.parseInt(request.getParameter("seq"));

- Controller에서는 seq를 받아 숫자로 변환

String sql = " DELETE FROM MVCBOARD WHERE SEQ=? ";

- 삭제에 필요한 것은 게시글 번호 한 개이므로 한 개만 받아온다

url = ?command=boardlist

- 삭제 성공 시 목록으로 이동하도록 url 설정