스프링09_게시판, 에러페이지

charl hi·2022년 1월 25일
0

Spring

목록 보기
10/25

게시판 만들기

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) {
		//Service 가져오기
		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) {
		//Service 가져오기
		List<BoardDto> list = boardService.selectList();
		//화면에 전달
//		model.addAttribute("list", list);
//		return "board/list";

		//model
		mv.addObject("list", list);
		//view
		mv.setViewName("board/list");
		return mv;
	}
	
	//상세 페이지 조회
//	@GetMapping("detail/{title}/{zzz}") 여러개도 가능하다!
	@GetMapping("detail/{title}")
//	나중에 번호로 가져오자
	public String detail(@PathVariable String title, Model model/*, @PathVariable String zzz*/) {
		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) {
		//Service
		int result = boardService.edit(b);
		if(result>0) {
			return "redirect:/board/list";
		}else {
			return "common/error/fail";
		}
	}
	
	//상세 페이지에서 삭제
	@PostMapping("delete")
	public String delete(BoardDto b) {
		//Service
		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
//@Component
public class BoardServiceImpl implements BoardService {

	@Autowired
	private BoardRepository boardRepository;
	
	//게시글 등록
	@Override
	public int insertBoard(BoardDto b) throws Exception {
		//Repository 가져오기
		return boardRepository.insert(b);
	}

	//게시글 전체 목록 조회
	@Override
	public List<BoardDto> selectList() {
		//Repository 가져오기
		return boardRepository.selectList();
	}

	//게시글 수정
	@Override
	public int edit(BoardDto b) {
		//Repository 가져오기
		return boardRepository.edit(b);
	}

	//게시글 삭제
	@Override
	public int delete(BoardDto b) {
		//Repository 가져오기
		return boardRepository.delete(b);
	}
}



BoardRepository

BoardRepository (인터페이스)

public interface BoardRepository {

	//insert
	public abstract int insert(BoardDto b) throws Exception;

	//selectAll
	public abstract List<BoardDto> selectList();

	//edit
	public abstract int edit(BoardDto b);

	//delete
	public abstract int delete(BoardDto b);
}


BoardRepositoryImpl (구현)

@Repository
//@Component 가능
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}
		<!-- 단일데이터 string, int... 이기 때문에 #{} 안에 아무거나 써도 된다! -->
	</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>
	<!-- jsp context root -->
	<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>
	<!-- action="" 현재 경로 그대로: app15/member/login -->
	<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>
	
	<!-- <error-page>
		<exception-type>java.lang.ArithmeticException</exception-type>
		<location>/error404.jsp</location>
	</error-page> -->

0개의 댓글