IT/Spring

[spring 게시판 만들기 #5] 게시판 등록 Create

jaewon_sss 2020. 9. 14. 11:58
반응형


스프링 게시판 등록하기




지난번 list.jsp 를 만들었던 board 폴더에 게시글 등록을 위한 create.jsp 파일을 생성




board/create 로 url 이동하는 controller 작성



BoardController 코드


// 게시물 작성 페이지로 이동
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String getcreate() throws Exception {
return "board/create";
}



잘 이동되는 것을 확인



create.jsp 코드

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<style>
.center{
margin: 5px 25px; padding: 20px
}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>Create</title>
</head>
<body>
<form class="center" method="post">
<h2>Create Page</h2>
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" name="id" placeholder="ID 작성해주세요.">
</div>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" placeholder="NAME 적어주세요.">
</div>
<div class="form-group">
<label>Goal</label>
<input class="form-control" name="goal" placeholder="GOAL 적어주세요.">
</div>
<button type="submit" class="btn btn-outline-info">등록</button>
<button type="button" class="btn btn-outline-info"><a href="/board/list">돌아가기</a></button>
</form>
</body>
</html>


게시글 등록 화면



쿼리문을 mapper 에 작성


boardMapper 코드

<!-- 게시물 등록 -->
<insert id="create">
insert into i_can_do_it (id,name,goal,date,time)
values (#{id}, #{name}, #{goal}, NOW(),NOW())
</insert>



한줄 추가




빨간색 부분 추가

(@override도 추가)




boardService 코드 추가




boardServiceImpl 코드 추가




list.jsp 에 글쓰기 버튼을 클릭하면 board/create url 로 이동하는 버튼 생성



list.jsp 에 추가할 코드

<a href="<c:url value='/board/create'/>" role="button" class="btn btn-outline-info">글쓰기</a>


이동은 되었으니 이제 등록할 수 있는 controller 생성


Boardcontroller 추가 코드

// 게시물 작성 post
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String postcreate(BoardVO vo) throws Exception {
service.create(vo);
return "redirect:list";
}



글쓰기를 클릭하면



create.jsp 코드를 반영한 화면이 나오고


등록하면




잘 등록이 되는것을 확인할 수 있다.

반응형