게시판 만들기
BoardController
@Controller
@RequestMapping("board")
public class BoardController {
@Autowired
private SqlSession sqlSession;
@Autowired
private BoardService boardService;
@GetMapping("insert")
public String insert() {
return "board/insert";
}
@PostMapping("insert")
public String insert(BoardDto b) {
int result = 0;
try {
result = boardService.insertBoard(b);
} catch (Exception e) {
e.printStackTrace();
return "redirect:/common/error/fail";
}
if(result>0) {
return "redirect:/board/list";
}else {
return "redirect:/common/error/fail";
}
}
@GetMapping("list")
public ModelAndView list(ModelAndView mv) {
List<BoardDto> list = boardService.selectList();
mv.addObject("list", list);
mv.setViewName("board/list");
return mv;
}
@GetMapping("detail/{title}")
public String detail(@PathVariable String title, Model model) {
BoardDto b = sqlSession.selectOne("board.selectOneByTitle", title);
System.out.println("selectOne : " + b);
model.addAttribute("data", b);
return "board/detail";
}
@PostMapping("edit")
public String edit(BoardDto b) {
int result = boardService.edit(b);
if(result>0) {
return "redirect:/board/list";
}else {
return "common/error/fail";
}
}
@PostMapping("delete")
public String delete(BoardDto b) {
int result = boardService.delete(b);
if(result>0) {
return "redirect:/board/list";
}else {
return "common/ error/fail";
}
}
}
BoardDto
public class BoardDto {
private String title;
private String content;
private String writer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
@Override
public String toString() {
return "BoardDto [title=" + title + ", content=" + content + ", writer=" + writer + "]";
}
}
BoardService
BoardService (인터페이스)
public interface BoardService {
public abstract int insertBoard(BoardDto b) throws Exception;
public abstract List<BoardDto> selectList();
public abstract int edit(BoardDto b);
public abstract int delete(BoardDto b);
}
BoardServiceImpl (구현)
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardRepository boardRepository;
@Override
public int insertBoard(BoardDto b) throws Exception {
return boardRepository.insert(b);
}
@Override
public List<BoardDto> selectList() {
return boardRepository.selectList();
}
@Override
public int edit(BoardDto b) {
return boardRepository.edit(b);
}
@Override
public int delete(BoardDto b) {
return boardRepository.delete(b);
}
}
BoardRepository
BoardRepository (인터페이스)
public interface BoardRepository {
public abstract int insert(BoardDto b) throws Exception;
public abstract List<BoardDto> selectList();
public abstract int edit(BoardDto b);
public abstract int delete(BoardDto b);
}
BoardRepositoryImpl (구현)
@Repository
public class BoardRepositoryImpl implements BoardRepository {
@Autowired
private SqlSession sqlSession;
@Override
public int insert(BoardDto b) throws Exception {
return sqlSession.insert("board.insert", b);
}
@Override
public List<BoardDto> selectList() {
return sqlSession.selectList("board.selectAll");
}
@Override
public int edit(BoardDto b) {
return sqlSession.update("board.update", b);
}
@Override
public int delete(BoardDto b) {
return sqlSession.delete("board.delete", b);
}
}
board-mapper.xml
<mapper namespace="board">
<insert id="insert" parameterType="boardDto">
INSERT INTO BOARD
VALUES(#{title}, #{content}, #{writer})
</insert>
<select id="selectAll" resultType="boardDto">
SELECT * FROM BOARD
</select>
<select id="selectOneByTitle" parameterType="string" resultType="boardDto">
SELECT * FROM BOARD
WHERE TITLE = #{zzztitle}
</select>
<update id="update" parameterType="boardDto">
UPDATE BOARD
SET
CONTENT = #{content}
WHERE TITLE = #{title}
</update>
<delete id="delete" parameterType="boardDto">
DELETE FROM BOARD
WHERE TITLE = #{title}
</delete>
</mapper>
뷰
board
detail.jsp
<body>
<h1>게시글 상세 페이지</h1>
<hr>
<form method="post">
<h2>제목 : <input type="text" name="title" value="${data.title}" readonly></h2><hr>
<span>작성자 : ${data.writer}</span><hr>
<div>
내용<br>
<input type="text" name="content" value="${data.content}">
</div><hr>
<input type="submit" value="수정하기" formaction="<%=request.getContextPath()%>/board/edit">
<input type="submit" value="삭제하기" formaction="<%=request.getContextPath()%>/board/delete">
</form>
</body>
insert.jsp
<body>
<h1>게시글 작성</h1>
<form action="" method="post">
제목 : <input type="text" name="title"><br>
내용 : <input type="text" name="content"><br>
작성자 : <input type="text" name="writer"><br>
<input type="submit" value="작성하기">
</form>
</body>
list.jsp
<body>
<h1>게시글 목록 페이지</h1>
전체 글 개수 : ${list.size()}<br>
<table border="1">
<thead>
<tr>
<th>제목</th>
<th>내용</th>
<th>작성자</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="l">
<tr>
<td><a href="detail/${l.title }">${l.title}</a></td>
<td>${l.content}</td>
<td>${l.writer}</td>
</tr>
</c:forEach>
</tbody>
</table>
<br>
<a href="insert">게시글 등록</a>
</body>
member
join.jsp
<body>
<h1>회원가입 페이지</h1>
<form action="join" method="post">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pwd"><br>
닉네임 : <input type="text" name="nick"><br>
주소 : <input type="text" name="addr"><br>
나이 : <input type="number" min="0" name="age"><br>
<input type="submit" value="가입하기">
</form>
</body>
login.jsp
<body>
<h1>로그인 화면</h1>
<form action="" method="post">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pwd"><br>
<input type="submit" value="로그인">
</form>
</body>
views > home.jsp
<body>
<h1>홈 화면</h1>
<% if(session.getAttribute("loginUser") != null){ %>
<h1>${loginUser.id}님 환영합니다!</h1>
<h1>${loginUser.nick}님 환영합니다!</h1>
<h1>나이 : ${loginUser.age}</h1>
<a href="member/logout">로그아웃</a>
<% }else{ %>
로그인 해주세요!
<a href="member/login">로그인페이지</a>
<% } %>
<br>
<a href="board/insert">게시글 등록</a>
</body>
에러 처리
webapp > error404.jsp
webapp > WEB-INF > web.xml
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>