과제 01) 없는 번호, 잘못된 번호 입력시
에러페이지가 출력되도록 해주세요!!!
+) web.xml 에러페이지 설정으로도 해보세요!~~
1번 코드
//FrontController.java
// 3. 응답(페이지 이동 등)
// 1) 전달할 데이터가 있니? 없니? == 포워드? 리다이렉트?
// 2) 어디로 갈까? == 경로
if(forward == null) {
// command 요청이 없는 경우
try {
response.sendRedirect("error/error.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
if(forward.isRedirect()) {
try {
response.sendRedirect(forward.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
RequestDispatcher dispatcher=request.getRequestDispatcher(forward.getPath());
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//BoardPageAction.jsp
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
BoardDAO boardDAO = new BoardDAO();
BoardDTO data = new BoardDTO();
int bid = Integer.parseInt(request.getParameter("bid"));
data.setBid(bid);
data = boardDAO.selectOne(data);
request.setAttribute("data", data);
ActionForward forward = new ActionForward();
if(data != null) {
forward.setPath("board.jsp");
}
else {
forward.setPath("error/error.jsp");
}
forward.setRedirect(false);
return forward;
}
2번 web.xml 설정
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0">
<error-page>
<error-code>404</error-code>
<location>/error/404error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error/500error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/error/500error.jsp</location>
</error-page>
</web-app>
과제 02) 특정 글에 대한 댓글목록을 출력해주세요!~~~
+ 커스텀태그 활용해보기~~
1번 java 및 jsp
// BoardPageAction.java
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
BoardDAO boardDAO = new BoardDAO();
BoardDTO data = new BoardDTO();
ReplyDAO replyDAO = new ReplyDAO();
ReplyDTO replyDTO = new ReplyDTO();
int bid = Integer.parseInt(request.getParameter("bid"));
data.setBid(bid);
replyDTO.setBid(bid);
ArrayList<ReplyDTO> datas = replyDAO.selectAll(replyDTO);
data = boardDAO.selectOne(data);
request.setAttribute("data", data);
request.setAttribute("datas", datas);
ActionForward forward = new ActionForward();
forward.setPath("board.jsp");
if(data == null){
forward.setPath("main.do");
}
forward.setRedirect(false);
return forward;
}
${data.bid}<br>
${data.title}<br>
${data.content}<br>
${data.writer}<br>
${data.regtime}<br>
${data.cnt}<br>
<hr>
<c:forEach var="reply" items="${datas}">
${reply.writer} | ${reply.content}<br>
</c:forEach>
<hr>
<a href="main.do">메인으로 돌아가기</a>
2번 커스텀 태그 사용
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %>
<mytag:board data="${data}"></mytag:board>
<hr>
<mytag:board_reply datas="${datas}"></mytag:board_reply>
<hr>
<a href="main.do">메인으로 돌아가기</a>
<!-- board.tag -->
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="data" type="java.lang.Object"%>
${data.bid}<br>
${data.title}<br>
${data.content}<br>
${data.writer}<br>
${data.regtime}<br>
${data.cnt}<br>
<!-- board_reply.tag -->
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="datas" type="java.lang.Object"%>
<c:forEach var="reply" items="${datas}">
${reply.writer} | ${reply.content}<br>
</c:forEach>
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
[Spring] Page 작성 과제(BOARD 관련) (0) | 2024.10.05 |
---|---|
[Spring] Page 작성 과제(MEMBER 관련) (0) | 2024.10.05 |
jSP 쇼핑몰 제작(page : controller) (0) | 2024.08.11 |
jSP 쇼핑몰 제작(page : cart, join, product) (0) | 2024.08.11 |
jSP 쇼핑몰 제작(page : index, main, mypage, admin) (0) | 2024.08.11 |