[간단한 게시판 만들기] - 3. 게시글 쓰기
웹/백엔드, Spring

[간단한 게시판 만들기] - 3. 게시글 쓰기

  1. BoardDto 빈 만들기
  2. write.jsp 페이지 만들기
  3. mvc패턴 순서대로 데이터 전달
  4. dao에서 DB연결 쿼리문 생성
  5. 잘 들어갔나 확인

 

BoardDto 빈 만들기

package com.web.toy.model;

public class BoardDto {
	private int no;
	private String userId;
	private String subject;
	private String content;
	private String rogtime;
	public BoardDto() {
		// TODO Auto-generated constructor stub
	}
	public BoardDto(int no, String userId, String subject, String content, String rogtime) {
		super();
		this.no = no;
		this.userId = userId;
		this.subject = subject;
		this.content = content;
		this.rogtime = rogtime;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getRogtime() {
		return rogtime;
	}
	public void setRogtime(String rogtime) {
		this.rogtime = rogtime;
	}
	@Override
	public String toString() {
		return "BoardDto [no=" + no + ", userId=" + userId + ", subject=" + subject + ", content=" + content
				+ ", rogtime=" + rogtime + "]";
	}
	
}

 

 

write.jsp 페이지 만들기

대강 부트스트랩에서 아무거나 복붙한다

 

mvc패턴을 만든다

view -> controller -> service -> dao ->db 순으로 위의 데이터를 전달 한다.

 

서블릿

private void write(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
		HttpSession session = request.getSession();
		Member member = (Member) session.getAttribute("userinfo");
		String path="/main.jsp";
		if(member != null) {
			String subject = request.getParameter("subject");
			String content = request.getParameter("content");
			//board객체 삽입
			BoardDto boardDto = new BoardDto();
			boardDto.setUserId(member.getUserid());
			boardDto.setSubject(subject);
			boardDto.setContent(content);
			BoardServiceImp.getBoardServiceImp().write(boardDto);
		}else {
			request.setAttribute("msg", "로그인을 해주세요");
			path = "/index.jsp";
		}
		request.getRequestDispatcher(path).forward(request, response);
	}

service

public void write(BoardDto boardDto) throws SQLException {
		Connection conn = DBUtil.getConnection();
		BoardDaoImp.getBoardDaoImp().write(conn, boardDto);
		DBUtil.close(conn);
		return;
	}

boardDao의 write메소드를 생성

	public void write(Connection conn, BoardDto boardDto) {

		PreparedStatement ps = null;
		try {
			String sql = "insert into guestbook(userid, subject, content, regtime) values(?,?,?,now()); ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, boardDto.getUserId());
			ps.setString(2, boardDto.getContent());
			ps.setString(3, boardDto.getSubject());
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			DBUtil.close(ps);
		}
	}

 

db에 잘 들어갔나 확인

성공!