728x90
반응형
index 파일 만들기
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- command라는 변수를 이용해서 어디로 가고싶은지 확인하고 응답시켜줄 것 -->
<a href = "mvccontroller.jsp?command=list">List...</a>
<!-- command라는 변수에 list(문자열)를 담아서 넘겨줌?
list문자열을 확인해서 맞으면 mvclist로 넘겨주기 위한 용도로 사용한 것?
-->
</body>
</html>
Jsp파일 만들기
1. mvccontroller.jsp
<%@page import="com.mvc.dto.mvcDto"%>
<%@page import="java.util.List"%>
<%@page import="com.mvc.biz.mvcBizImpl"%>
<%@page import="com.mvc.biz.mvcBiz"%>
<%@page import="com.mvc.dao.mvcDao"%>
<%@page import="com.mvc.dao.mvcDaoImpl"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String command = request.getParameter("command");
mvcBiz biz = new mvcBizImpl();
System.out.println("<" + command + ">");
/*
Controller에서 할일!
1. (만일 이전 페이지에서 값을 전달했다면) 값 받아주기
2. (만일 DB와 연결해야 한다면) DB와 연결하기
3. (만일 다음페이지에 값을 전달해야 한다면) request에 값 저장하기
4. 페이지 이동하기
*/
if(command.equals("list")){ //페이지를 넘기기 위해 command에 담아준 문자열이 list가 맞는지 확인
// 1. X
// 2.
List<mvcDto> list = biz.selectList(); //list가 맞다면 DB에서 자료를 가져와서 배열 list에 담아줘
// 3.
request.setAttribute("list", list); //(다형성)request객체에 왼쪽이름으로 오른쪽 object를 담아서 전달해줄거야
// 4.
pageContext.forward("mvclist.jsp"); //.getparameter는 변수는 값! 형태로 setAttribute랑 좀 다름
//pageContext는 jsp기본 객체로 jsp페이지에서 따로 선언하지 않아도 참조하여 사용 가능
//jsp페이지에 1:1로 연결된 객체로 jsp페이지당 하나의 pageContext 객체가 생성됨
//forward: 지정한 상대경로 페이지로 이동
} else if (command.equals("insertform")){
// 1. X
// 2. X
// 3. X
// 4.
response.sendRedirect("mvcinsert.jsp");
} else if(command.equals("insertres")){
// 1.
String writer = request.getParameter("writer");
String title = request.getParameter("title");
String content = request.getParameter("content");
mvcDto dto = new mvcDto(writer, title, content);
// 2.
// 3. X
if(biz.insert(dto)){
// 4.
%>
<script type="text/javascript">
alert("글 작성 성공");
location.href="mvccontroller.jsp?command=list";
</script>
<%
} else {
%>
<script type="text/javascript">
alert("글 작성 실패");
location.href="mvccontroller.jsp?command=insertform";
</script>
<%
}
} else if(command.equals("selectone")){
// 1.
int seq = Integer.parseInt(request.getParameter("seq"));
// 2.
mvcDto dto = biz.selectOne(seq);
// 3.
request.setAttribute("dto", dto);
// 4.
pageContext.forward("mvcselect.jsp");
} else if(command.equals("updateform")){
// 1.
int seq = Integer.parseInt(request.getParameter("seq"));
// 2.
mvcDto dto = biz.selectOne(seq);
// 3.
request.setAttribute("dto", dto);
// 4.
pageContext.forward("mvcupdate.jsp");
} else if(command.equals("updateres")){
// 1.
String title = request.getParameter("title");
String content = request.getParameter("content");
int seq = Integer.parseInt(request.getParameter("seq"));
mvcDto dto = new mvcDto(seq, title, content);
//2.
if(biz.update(dto)){
//3. X
//4.
%>
<script type="text/javascript">
alert("글 수정 성공");
location.href="mvccontroller.jsp?command=selectone&seq=<%=seq%>";
</script>
<% } else {
%>
<script type="text/javascript">
alert("글 수정 실패");
location.href="mvcupdate.jsp?command=update&seq=<%=dto.getSeq()%>"
</script>
<%
}
} else if(command.equals("deleteform")){
//1.
int seq = Integer.parseInt(request.getParameter("seq"));
System.out.println("<" + seq + ">");
//2.
if(biz.delete(seq)){
%>
<script type="text/javascript">
alert("글 삭제 성공");
location.href="mvccontroller.jsp?command=list";
</script>
<% } else {
%>
<script type="text/javascript">
alert("글 삭제 실패");
location.href="mvccontroller.jsp?command=selectone&seq=<%=seq%>";
</script>
<% }
}
%>
<h1 style="color:red;">잘못왔다!</h1>
</body>
</html>
2. mvclist.jsp
<%@page import="com.mvc.dto.mvcDto"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//list에 담은 object타입의 list를 꺼내서 다시 담음(다형성)
//object list는 부모객체 List는 자식객체 따라서 명시적 형변환 필요
List<mvcDto> list = (List<mvcDto>)request.getAttribute("list");
%>
<h1>LIST</h1>
<table border="1">
<col width="50px"/>
<col width="100px"/>
<col width="300px"/>
<col width="100px"/>
<tr>
<th>번호</th>
<th>작성자</th>
<th>제목</th>
<th>작성일</th>
</tr>
<%
if(list.size() == 0){
%>
<tr>
<td colspan="4">---------------작성된 글이 존재하지 않습니다.---------------</td>
</tr>
<%
} else {
for(mvcDto dto: list){
%>
<tr align="center">
<td><input type="hidden" name="seq" value="<%=dto.getSeq()%>"/><%=dto.getSeq() %></td> <!-- server에서 이미 컴파일했기때문에 F12 개발자도구에 안보임 -->
<td><%=dto.getWriter() %></td>
<td><a href="mvccontroller.jsp?command=selectone&seq=<%=dto.getSeq()%>"><%=dto.getTitle() %></a></td>
<td><%=dto.getRegdate() %></td>
</tr>
<%
}
}
%>
<tr>
<td colspan="4" align="right">
<input type="button" value="글작성" onclick="location.href='mvccontroller.jsp?command=insertform'"/>
</td>
</tr>
</table>
</body>
</html>
3. mvcselect.jsp
<%@page import="com.mvc.dto.mvcDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
mvcDto dto = (mvcDto)request.getAttribute("dto");
%>
<h1>DETAIL</h1>
<input type="hidden" name="seq" value="<%=dto.getSeq() %>"/>
<table border="1">
<col width="100px"/>
<col width="600px"/>
<tr>
<th>작성자</th>
<td><%=dto.getWriter() %></td>
</tr>
<tr>
<th>제목</th>
<td><%=dto.getTitle() %></td>
</tr>
<tr>
<th>내용</th>
<td><%=dto.getContent() %></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="수정" onclick="location.href='mvccontroller.jsp?command=updateform&seq=<%=dto.getSeq()%>'"/>
<input type="button" value="삭제" onclick="location.href='mvccontroller.jsp?command=deleteform&seq=<%=dto.getSeq()%>'"/>
<input type="button" value="목록" onclick="location.href='mvccontroller.jsp?command=list'"/>
</td>
</tr>
</table>
</body>
</html>
4. mvcinsert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>INSERT</h1>
<form action="mvccontroller.jsp" method="post"><!-- name이 query string으로 내부적으로 만들어져서 넘어감 ?command 쓰지 말자 -->
<input type="hidden" name="command" value="insertres"/>
<table border="1">
<tr>
<th>작성자</th>
<td><input type="text" name="writer"/></td>
</tr>
<tr>
<th>제목</th>
<td><input type="text" name="title"/></td>
</tr>
<tr>
<th>내용</th>
<td><textarea rows="10" cols="60" name="content"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='mvccontroller.jsp?command=list'"/>
<input type="submit" value="작성"/>
</td>
</tr>
</table>
</form>
</body>
</html>
5. mvcupdate.jsp
<%@page import="com.mvc.dto.mvcDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
mvcDto dto = (mvcDto)request.getAttribute("dto");
%>
<h1>UPDATE</h1>
<form action="mvccontroller.jsp" method="post">
<input type="hidden" name="command" value="updateres"/>
<input type="hidden" name="seq" value="<%=dto.getSeq()%>"/>
<table border="1">
<col width="100px"/>
<col width="600px"/>
<tr>
<th>작성자</th>
<td><input type="text" name="writer" value="<%=dto.getWriter()%>" readonly="readonly"/></td>
</tr>
<tr>
<th>제목</th>
<td><input type="text" name="title" value="<%=dto.getTitle()%>"/></td>
</tr>
<tr>
<th>내용</th>
<td><textarea rows="10" cols="60" name="content"><%=dto.getContent()%></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='mvccontroller.jsp?command=list'"/>
<input type="submit" value="수정" />
</td>
</tr>
</table>
</form>
</body>
</html>
728x90
반응형
'Web > Jsp_servlet' 카테고리의 다른 글
[JSP]로그인만들기_01 (0) | 2020.08.13 |
---|---|
[JSP]forward와 redirect (0) | 2020.08.12 |
[JSP]MVC2게시판만들기_01 (0) | 2020.08.12 |
[JSP]MVC게시판만들기_02 (0) | 2020.08.06 |
[JSP]MVC게시판 만들기_01 (0) | 2020.08.06 |