관리 메뉴

개발 노트

10/24 spring MVC 본문

프로젝트 기반 JAVA 응용 SW개발 : 22.07.19~23.01.20/Spring

10/24 spring MVC

hayoung.dev 2022. 11. 1. 17:17

spring에서는 주고받는 사이에 model 객체를 쓰는 것이 권장이다.

 

(중요 : 면접질문)

인터페이스의 장점 : 표준화, 모듈화 할 때 편하다.

 

<och07_mvcboard > (10/23~ 이어짐)

java > dto > BDto.java

package com.oracle.oMVCBoard.dto;

import java.sql.Timestamp;

public class BDto {
	int bId;
	String bName;
	String bTitle;
	String bContent;
	Timestamp bDate;
	int bHit;
	int bGroup;
	int bStep;
	int bIndent;
	
	public BDto() {
		// TODO Auto-generated constructor stub
	}
	
	public BDto(int bId,     String bName, String bTitle, String bContent,
			    Timestamp    bDate,        int bHit,
			    int bGroup,  int bStep,    int bIndent) {
		// TODO Auto-generated constructor stub
		this.bId = bId;
		this.bName = bName;
		this.bTitle = bTitle;
		this.bContent = bContent;
		this.bDate = bDate;
		this.bHit = bHit;
		this.bGroup = bGroup;
		this.bStep = bStep;
		this.bIndent = bIndent;
	}

	public int getbId() {
		return bId;
	}

	public void setbId(int bId) {
		this.bId = bId;
	}

	public String getbName() {
		return bName;
	}

	public void setbName(String bName) {
		this.bName = bName;
	}

	public String getbTitle() {
		return bTitle;
	}

	public void setbTitle(String bTitle) {
		this.bTitle = bTitle;
	}

	public String getbContent() {
		return bContent;
	}

	public void setbContent(String bContent) {
		this.bContent = bContent;
	}

	public Timestamp getbDate() {
		return bDate;
	}

	public void setbDate(Timestamp bDate) {
		this.bDate = bDate;
	}

	public int getbHit() {
		return bHit;
	}

	public void setbHit(int bHit) {
		this.bHit = bHit;
	}

	public int getbGroup() {
		return bGroup;
	}

	public void setbGroup(int bGroup) {
		this.bGroup = bGroup;
	}

	public int getbStep() {
		return bStep;
	}

	public void setbStep(int bStep) {
		this.bStep = bStep;
	}

	public int getbIndent() {
		return bIndent;
	}

	public void setbIndent(int bIndent) {
		this.bIndent = bIndent;
	}
   }

java > controller > BController.java

package com.oracle.oMVCBoard.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oracle.oMVCBoard.command.BCommand;
import com.oracle.oMVCBoard.command.BContentCommand;
import com.oracle.oMVCBoard.command.BDeleteCommand;
import com.oracle.oMVCBoard.command.BListCommand;
import com.oracle.oMVCBoard.command.BModifyCommand;
import com.oracle.oMVCBoard.command.BReplyCommand;
import com.oracle.oMVCBoard.command.BReplyViewCommand;
import com.oracle.oMVCBoard.command.BWriteCommand;

@Controller
public class BController {
	private static final Logger logger = LoggerFactory.getLogger(BController.class);
	
	BCommand command = null; /* BCommand.java 사용 */
	
	@RequestMapping("list")
	public String list(Model model) {
        logger.info("list start...");
		command = new BListCommand(); /* BListCommand.java 사용*/
		command.execute(model); /* BListCommand.java에서 담은 model값이 들어온다. */
		return "list"; /* 그 값을 가지고 list.jsp로 가져간다. */

	}
	
	
	@RequestMapping("/content_view")
	public String content_view(HttpServletRequest request, Model model){
		System.out.println("content_view()");
		/* "request"가 이름, request가 객체 */
		model.addAttribute("request", request);
		/* BContentCommand.java */
		command = new BContentCommand(); 
		command.execute(model);
		
		return "content_view";
	}
	
	/* model에 담아서 보내기 때문에 HttpServletRequest request를 사용한다. */
	@RequestMapping(value = "/modify" , method =RequestMethod.POST)
	public String modify(HttpServletRequest request , Model model) {
		logger.info("modify start..");
		model.addAttribute("request", request);
		/* BModifyCommand.java */
		command = new BModifyCommand();  
		command.execute(model);

		/* redirect 사용 : 메소드 간의 수정이 발생하면 list로 간다. */
		return "redirect:list";
	}
	
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request, Model model) {
		System.out.println("delete()");
		
		model.addAttribute("request", request);
		command = new BDeleteCommand();
		command.execute(model);
		
		return "redirect:list";
	}
		
	@RequestMapping("/write_view")
	public String write_view(Model model) {
		logger.info("write_view start..");
		
		return "write_view";
	}

	@PostMapping(value = "/write")
	public String write(HttpServletRequest request, Model model) {
		logger.info("write start..");
		
		model.addAttribute("request", request);
		
		command = new BWriteCommand();
		command.execute(model);
	
		return "redirect:list";
	}

	@RequestMapping("/reply_view")
	public String reply_view(HttpServletRequest request, Model model){
		System.out.println("reply_view start..");
		
		model.addAttribute("request", request);
		command = new BReplyViewCommand(); 
		command.execute(model);
		return "reply_view";
	}
	
	@RequestMapping(value="/reply",  method=RequestMethod.POST )
	public String reply(HttpServletRequest request, Model model) {
		System.out.println("reply()");
		
		model.addAttribute("request", request);		
		command = new BReplyCommand();
		command.execute(model);
		
		return "redirect:list";
	}

}

java > command > BCommand.java

package com.oracle.oMVCBoard.command;

import org.springframework.ui.Model;

public interface BCommand {
	void execute(Model model);
}

java > command > BListCommand.java

package com.oracle.oMVCBoard.command;

import java.util.ArrayList;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;

// Service
public class BListCommand implements BCommand {

	@Override
	public void execute(Model model) {
		// Dao 연결 
		BDao dao = new BDao();
		ArrayList<BDto> boardDtoList = dao.boardList(); /* BDao.java의 board.List */
		System.out.println("BListCommand boardDtoList.size()-->"+boardDtoList.size());
		/* call by reference 때문에 boardDtoList의 리턴값이 model에 담긴다. */
		model.addAttribute("boardList", boardDtoList);
		

	}

}

java > dao > BDao.java

package com.oracle.oMVCBoard.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.oracle.oMVCBoard.dto.BDto;

public class BDao {
	DataSource  dataSource;
	public   BDao() {
		
		try {
			Context  context = new InitialContext();
			dataSource = (DataSource) context.lookup("java:comp/env/jdbc/OracleDB");
		} catch (NamingException e) {
			System.out.println("생성자 dataSource-->" + e.getMessage() );
			e.printStackTrace();
		}
		
	}
	public ArrayList<BDto> boardList() {
		ArrayList<BDto> bList = new ArrayList<BDto>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		System.out.println("BDao boardList start..." );
		
		try {
			connection = dataSource.getConnection();
			String query = "SELECT bId, bName, bTitle, bContent, bDate, bHit,"
					+ "            bGroup, bStep, bIndent "
					+ "     FROM   mvc_board order by bGroup desc, bStep asc";
			preparedStatement = connection.prepareStatement(query);
			System.out.println("BDao  query-->" + query );
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				int bId = resultSet.getInt("bId");
				String bName = resultSet.getString("bName");
				String bTitle = resultSet.getString("bTitle");
				String bContent = resultSet.getString("bContent");
				Timestamp bDate = resultSet.getTimestamp("bDate");
				int bHit = resultSet.getInt("bHit");
				int bGroup = resultSet.getInt("bGroup");
				int bStep = resultSet.getInt("bStep");
				int bIndent = resultSet.getInt("bIndent");
				/* setter를 위한 생성자 생성 */
				BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, 
						            bGroup, bStep, bIndent);
				bList.add(dto);
			}

		} catch (SQLException e) {
			System.out.println("list  dataSource-->" + e.getMessage() );
			e.printStackTrace();
		} finally {
			try {
				if(resultSet != null) resultSet.close();
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}		
		
		return bList;
	}
	
	public BDto contentView(String strId) {
		upHit(strId); 
		BDto dto = null; /* return dto이기 때문에 초기화 작업 해놓는다.*/
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		try {
			connection = dataSource.getConnection();
			String query = "select * from mvc_board where bId = ?";
			preparedStatement = connection.prepareStatement(query);
			//요즘에는  parsing 안해도 자동으로 처리된다.
			preparedStatement.setInt(1, Integer.parseInt(strId));
			resultSet = preparedStatement.executeQuery();

			if(resultSet.next()) {
				int bId = resultSet.getInt("bId");
				String bName = resultSet.getString("bName");
				String bTitle = resultSet.getString("bTitle");
				String bContent = resultSet.getString("bContent");
				Timestamp bDate = resultSet.getTimestamp("bDate");
				int bHit = resultSet.getInt("bHit");
				int bGroup = resultSet.getInt("bGroup");
				int bStep = resultSet.getInt("bStep");
				int bIndent = resultSet.getInt("bIndent");
				dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(resultSet != null) resultSet.close();
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
		return dto;
	}
	
	/* 한 번 글을 볼 때 조회수를 하나 올려줌 */
	/* Dao 내부에서만 볼 것이기 때문에 private로 해주는 것이 좋다. */
	private void upHit( String bId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = dataSource.getConnection();
			String query = "update mvc_board set bHit = bHit + 1 where bId = ?";
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, bId);
			
			int rn = preparedStatement.executeUpdate();
					
		} catch (Exception e) {
			// TODO: handle exception 
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
	
	public void modify(String bId, String bName, String bTitle, String bContent) {
		// TODO Auto-generated method stub
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = dataSource.getConnection();
			
			String query = "update mvc_board set bName = ?, bTitle = ?, "
				                              + "bContent = ? where bId = ?";
			System.out.println("BDao modify query->"+query);
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, bName);
			preparedStatement.setString(2, bTitle);
			preparedStatement.setString(3, bContent);
			preparedStatement.setInt(4, Integer.parseInt(bId));
			int rn = preparedStatement.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}

	public void delete(String bId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			
			connection = dataSource.getConnection();
			String query = "delete from mvc_board where bId = ?";
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, Integer.parseInt(bId));
			int rn = preparedStatement.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}

	
	public void write(String bName, String bTitle, String bContent) {
		// TODO Auto-generated method stub
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = dataSource.getConnection();
			String query = 
"Insert Into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent, bDate ) "
+ "Values (mvc_board_seq.nextval,?, ?, ?, 0, mvc_board_seq.currval,  0, 0 , sysdate)";
			/* 시퀀스에서 .currval 사용하면 방금 nextval로 생성한 번호를 그대로 쓸 수 있다. */ 
			System.out.println("BDao write  query-->" + query );
			
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, bName);
			preparedStatement.setString(2, bTitle);
			preparedStatement.setString(3, bContent);
			int rn = preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("write  dataSource-->" + e.getMessage() );
			e.printStackTrace();
		} finally {
			try { 
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
	}

	public BDto reply_view(String str) {
		BDto dto = null;
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			connection = dataSource.getConnection();
			String query = "select * from mvc_board where bId = ?";
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, Integer.parseInt(str));
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next()) {
				int bId = resultSet.getInt("bId");
				String bName = resultSet.getString("bName");
				String bTitle = resultSet.getString("bTitle");
				String bContent = resultSet.getString("bContent");
				Timestamp bDate = resultSet.getTimestamp("bDate");
				int bHit = resultSet.getInt("bHit");
				int bGroup = resultSet.getInt("bGroup");
				int bStep = resultSet.getInt("bStep");
				int bIndent = resultSet.getInt("bIndent");
				
				dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
			}
		} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
		} finally {
				try {
					if(resultSet != null)         resultSet.close();
					if(preparedStatement != null) preparedStatement.close();
					if(connection != null)        connection.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
		}
		
		return dto;
	}

	
	public void reply(String bId, String bName, String bTitle, String bContent, 
			String bGroup, 	String bStep, String bIndent) {
		// TODO Auto-generated method stub
		
		replyShape(bGroup, bStep);
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = dataSource.getConnection();
			String query = "insert into mvc_board (bId, bName, bTitle, bContent, "
				     	+ " bGroup, bStep, bIndent)"
					    + " values (mvc_board_seq.nextval, ?, ?, ?, ?, ?, ?)";
			preparedStatement = connection.prepareStatement(query);
			
			preparedStatement.setString(1, bName);
			preparedStatement.setString(2, bTitle);
			preparedStatement.setString(3, bContent);
			preparedStatement.setInt(4, Integer.parseInt(bGroup));
			preparedStatement.setInt(5, Integer.parseInt(bStep) + 1);
			preparedStatement.setInt(6, Integer.parseInt(bIndent) + 1);
			
			int rn = preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
	}

	private void replyShape( String strGroup, String strStep) {
		// TODO Auto-generated method stub
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = dataSource.getConnection();
			String query = "update mvc_board set bStep = bStep + 1 "
				     	+ " where bGroup = ? and bStep > ?";
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, Integer.parseInt(strGroup));
			preparedStatement.setInt(2, Integer.parseInt(strStep));
			
			int rn = preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exceptionu
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close(); 
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
}

views > list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 이렇게 해두면 header.jsp파일로 만든 디자인이 자동으로 따라온다. -->
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body>
	<h1>게시판</h1>
	<table>
		<tr>
			<td>번호</td><td>이름</td><td>제목</td><td>날짜</td>	<td>히트</td>
		</tr>
		<!-- BController.java에서 가져온 값을 boardList에 넣고 mvc_board로 forEach문으로 하나씩 뿌려준다. -->
	    <c:forEach items="${boardList }" var="mvc_board">
			<tr>
				<td>${mvc_board.bId}</td>
				<td>${mvc_board.bName}</td>
				<td>
					<c:forEach begin="1" end="${mvc_board.bIndent}">-</c:forEach>
								<!-- BController.java의 /content_view --> 
								<a href="content_view?bId=${mvc_board.bId}">${mvc_board.bTitle}</a></td>
							<td>${mvc_board.bDate}</td>
							<td>${mvc_board.bHit}</td>
						</tr>
		</c:forEach>
		<tr>
			<td colspan="5"> <a href="write_view">글작성</a> </td>
		</tr>
	</table>
</body>
</html>

views > header.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"    %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"   %>
<link href="resources/css/board.css" rel="stylesheet" type="text/css" >

java > command > BContentCommand.java (BCommand 상속)

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;

public class BContentCommand implements BCommand {

	@Override
	public void execute(Model model) {
		//asMap() : model을  map으로 전환해준다.
		//String에는 이름이, object에는 객체가 들어간다. 
		Map<String, Object> map = model.asMap();
		//request라는 이름을 호출해준 다음 request 객체로 넘어간다.
		/* 부모 = 자식인 경우에는 캐스팅을 해준다. */
		HttpServletRequest request = (HttpServletRequest /* 캐스팅 */) map.get("request");
		String bId = request.getParameter("bId"); 
		
		BDao dao = new BDao();
		BDto board = dao.contentView(bId);
		
		model.addAttribute("mvc_board", board);

	}

}

views > content_view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="header.jsp"   %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="modify" method="post">
		<input type="hidden" name="bId" value="${mvc_board.bId}">
	    <table border="1">
			<tr>
				<td> 번호 </td><td> ${mvc_board.bId} </td>
			</tr>
			<tr>
				<td> 히트 </td><td> ${mvc_board.bHit} </td>
			</tr>
			<tr>
				<td> 이름 </td><td><input type="text" name="bName" value="${mvc_board.bName}"></td>
			</tr>
			<tr>
				<td> 제목 </td>
				<td> <input type="text" name="bTitle" value="${mvc_board.bTitle}"></td>
			</tr>
			<tr>
				<td> 내용 </td>
				<td> <textarea rows="10" name="bContent" >${mvc_board.bContent}</textarea></td>
			</tr>
			<tr >
				<td colspan="2"> <input type="submit" value="수정"> &nbsp;&nbsp; 
				<a href="list">목록보기</a> &nbsp;&nbsp; 
				<a href="delete?bId=${mvc_board.bId}">삭제</a> &nbsp;&nbsp; 
				<a href="reply_view?bId=${mvc_board.bId}">답변</a></td>
			</tr>
	    </table>
	</form>
</body>
</html>

java > command > BModifyCommand.java

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;

public class BModifyCommand implements BCommand {

	@Override
	public void execute(Model model) {
		// 1. model Map선언
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		// 2. parameter ->  bId, bName , bTitle , bContent
		String bId = request.getParameter("bId");
		String bName = request.getParameter("bName");
		String bTitle = request.getParameter("bTitle");
		String bContent = request.getParameter("bContent");

		BDao  dao = new BDao();
		dao.modify(bId, bName, bTitle, bContent); 

	}

}

java > command > BDeleteCommand.java

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;

public class BDeleteCommand implements BCommand {

	@Override
	public void execute(Model model) {
	//	 1)  model이용 , map 선언
    //	 2) request 이용 ->  bId 추출
    //	 3) dao  instance 선언
    //	 4) delete method 이용하여 삭제
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		
		String bId = request.getParameter("bId");
		BDao dao = new BDao();
		dao.delete(bId);
	}

}

views > write_view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="header.jsp"   %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="write" method="post">
	   <table width="500" border="1">
			<tr>
				<td> 이름 </td>
				<td> <input type="text" name="bName" size = "50"> </td>
			</tr>
			<tr>
				<td> 제목 </td>
				<td> <input type="text" name="bTitle" size = "50"> </td>
			</tr>
			<tr>
				<td> 내용 </td>
				<td> <textarea name="bContent" rows="10" ></textarea> </td>
			</tr>
			<tr >
				<td colspan="2"> <input type="submit" value="입력"> &nbsp;&nbsp; 
				<a href="list">목록보기</a></td>
			</tr>
	   </table>
	</form>
</body>
</html>

scott 계정에서 하단처럼 시퀀스 이름만 작성하여 생성

시퀀스 생성 후 편집으로 테이블 데이터의 맨 끝의 번호를 [다음으로 시작] 숫자로 지정한다.

위 테이블은 43까지 있기 때문에  [다음으로 시작] 숫자를 44로 지정

 

java > command > BWriteCommand.java

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;

public class BWriteCommand implements BCommand {
 
	@Override
	public void execute(Model model) {
//	  1)  model이용 , map 선언
//	  2) request 이용 ->  bName  ,bTitle  , bContent  추출
//	  3) dao  instance 선언
//	  4) write method 이용하여 저장(bName, bTitle, bContent)

		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		String bName = request.getParameter("bName");
		String bTitle = request.getParameter("bTitle");
		String bContent = request.getParameter("bContent");
		
		BDao dao = new BDao();
		dao.write(bName, bTitle, bContent);
	}

}

views > reply_view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="header.jsp"   %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="reply" method="post">
		<input type="hidden" name="bId"     value="${reply_view.bId}">
		<input type="hidden" name="bGroup"  value="${reply_view.bGroup}">
		<input type="hidden" name="bStep"   value="${reply_view.bStep}">
		<input type="hidden" name="bIndent" value="${reply_view.bIndent}">
	    <table width="500"  border="1">
			<tr>
				<td> 번호 </td><td>${reply_view.bId} </td>
			</tr>
			<tr>
				<td> 히트 </td><td>${reply_view.bHit} </td>
			</tr>
			<tr>
				<td> 이름 </td><td><input type="text" name="bName" value="${reply_view.bName}"></td>
			</tr>
			<tr>
				<td> 제목 </td><td><input type="text" name="bTitle" value="답변" +"${reply_view.bTitle}"></td>
			</tr>
			<tr>
				<td> 내용 </td>
				<td> <textarea rows="10"  name="bContent">${reply_view.bContent}</textarea></td>
			</tr>
			<tr >
				<td colspan="2"><input type="submit" value="답변저장"> 
				<a href="list" >목록</a></td>
			</tr>
	    </table>
	</form>
</body>
</html>

java > command > BReplyCommand.java

package com.oracle.oMVCBoard.command;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;

public class BReplyCommand implements BCommand {

	@Override
	public void execute(Model model) {
//	  1)  model이용 , map 선언
//	  2) request 이용 -> bid,         bName ,  bTitle,
//	                    bContent ,  bGroup , bStep ,
//	                    bIndent 추출
//	  3) dao  instance 선언
//	  4) reply method 이용하여 댓글저장 
//	    - dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		
		String bId = request.getParameter("bId");
		String bName = request.getParameter("bName");
		String bTitle = request.getParameter("bTitle");
		String bContent = request.getParameter("bContent");
		String bGroup = request.getParameter("bGroup");
		String bStep = request.getParameter("bStep");
		String bIndent = request.getParameter("bIndent");
		
		int bIntGroup = Integer.parseInt(request.getParameter("bGroup"));
        System.out.println("BReplyCommand bIntGroup->"+bIntGroup);
		
		BDao dao = new BDao();
		dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);


	}

}

java > command > BReplyViewCommand.java

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;

public class BReplyViewCommand implements BCommand {

	@Override
	public void execute(Model model) {
//	  1)  model이용 , map 선언
//	  2) request 이용 ->  bid  추출
//	  3) dao  instance 선언
//	  4) reply_view method 이용하여 (bid)
//	    - BDto dto = dao.reply_view(bId);
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		String bId = request.getParameter("bId");
		
		BDao dao = new BDao();
		BDto dto = dao.reply_view(bId); 
		
		model.addAttribute("reply_view", dto);

	}

}

 

 

출력 결과

 

글 클릭 시

 

답변 클릭 시

 

글 작성 클릭 시

반응형