고인돌 프로젝트/[Step 3] 모델 2(MVC) Spring boot
2. WEB 디렉토리 구조 잡기
고인돌개발자
2021. 8. 24. 18:46
▶ 참고 : 이 프로젝트는 | [Web 모델 1] Jsp,beans + Oracle | [Web 모델2] Servlet ,beans,jsp + Oracle 에 이어지는 프로젝트 입니다. 기존 진행했던 프로젝트를 먼저 진행하셔야 쉽게 따라올 수 있습니다.
1. web 디렉토리 구조
src - main - webapp 아래, 기존 Webcontents 에 있던 구조를 그대로 복사해서 넣습니다.
2. jsp 실행여부 테스트
☞ html, jsp 특히 jdbcTest.jsp 가 정상 작동하는지 확인합니다.
3. 소스코드
더보기
/src/main/webapp/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> ■모델 2 Spring </h1>
<a href="./html/regist.html">● 등록 </a>
<p>
<a href="/PeopleList">● 조회 (삭제)</a>
</body>
</html>
/src/main/webapp/html/regist.html
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> ■모델 2 Spring </h1>
<a href="/index.html">● Home </a>
<p>
<form name="regist" action="/RegistPeople" method="post">
<table style="width: 200px;">
<tr>
<td> 구분 </td> <td align="center">입력</td>
</tr>
<tr>
<td> ID </td> <td><input type="text" name="input_id" value="" /> </td>
</tr>
<tr>
<td> Name </td> <td><input type="text" name="input_name" value="" /></td>
</tr>
<tr>
<td> Age </td> <td><input type="text" name="input_age" value="" /> </td>
</tr>
</table>
<p>
<input type="submit" value="전송" style="width:200px;">
</form>
</body>
</html>
/src/main/webapp/jsp/jdbcTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String driver = "oracle.jdbc.driver.OracleDriver";
String dbURL="jdbc:oracle:thin:@localhost:1521:xe";
String user_id="scott";
String user_pw="tiger";
String qry="";
Connection conn = null; // DB 에 connection 된 객체를 저장
PreparedStatement ps = null; // connection 객체에 실행문을 던지는 역할(창구)
ResultSet rs = null; // select 결과값을 받아옮
try {
/* Driver Loading */
Class.forName(driver);
conn = DriverManager.getConnection(dbURL, user_id, user_pw);
conn.setAutoCommit(false); // 자동커밋 해제
System.out.println("Connection Success");
/* Result Set , Print */
qry = " select id, name, age, to_char(reg_date,'yyyy.mm.dd') as dati "
+" from people ";
ps = conn.prepareStatement(qry);
rs = ps.executeQuery();
while(rs.next()) {
out.println("ID : "+rs.getString("id"));
out.println("<br>");
out.println("Name : "+rs.getString("name"));
out.println("<br>");
out.println("age : "+rs.getString("age"));
}
}catch (Exception e) {
System.out.println("Error =>"+e);
conn.rollback();
}finally {
/* Close */
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(conn != null) conn.close();
}catch (Exception e2) {
}
}
%>
/src/main/webapp/WEB-INF/view/list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*, com.model2.spring.di.vo.*" %>
<%
List<People> list2 = (ArrayList<People>)request.getAttribute("people");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Select </title>
</head>
<body>
<h1> ■모델 2 , Spring - list.jsp</h1>
<a href="/index.html">● Home </a>
<p>
<table style="width: 400px;" border="0">
<tr style="height: 40px">
<td>ID</td>
<td align="center">이름</td>
<td align="center">나이</td>
<td align="center">등록일자</td>
<td> 삭제 </td>
</tr>
<%
String strId;
try{
for(People p : list2){
strId = p.getStrID();
%>
<tr style="height: 40px">
<td><%=strId%></td>
<td align="center"><%=p.getStrName()%></td>
<td align="center"><%=p.getStrAge()%></td>
<td align="center"><%=p.getStrDati()%></td>
<td><a href="/DeletePeople?id=<%=strId%>">삭제 </a></td>
</tr>
<%
}
}catch (Exception e) {
System.out.println("Error =>"+e);
}finally {
}
%>
</table>
</body>
</html>
/src/main/webapp/WEB-INF/view/list_lombok.jsp (lombok 적용시 사용)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*,com.model2.spring.di.vo.*" %>
<%
List<People_lombok> list2 = (ArrayList<People_lombok>)request.getAttribute("people");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Select </title>
</head>
<body>
<h1> ■모델 2 , Spring - list_lombok.jsp </h1>
<a href="/index.html">● Home </a>
<p>
<table style="width: 400px;" border="0">
<tr style="height: 40px">
<td>ID</td>
<td align="center">이름</td>
<td align="center">나이</td>
<td align="center">등록일자</td>
<td> 삭제 </td>
</tr>
<%
String strId;
try{
for(People_lombok p : list2){
strId = p.getStrID();
%>
<tr style="height: 40px">
<td><%=strId%></td>
<td align="center"><%=p.getStrName()%></td>
<td align="center"><%=p.getStrAge()%></td>
<td align="center"><%=p.getStrDati()%></td>
<td><a href="/DeletePeople?id=<%=strId%>">삭제 </a></td>
</tr>
<%
}
}catch (Exception e) {
System.out.println("Error =>"+e);
}finally {
}
%>
</table>
</body>
</html>