output/JSP

[수업] JSTL 응용 (명소, 자연)

hs_developer 2022. 7. 25. 16:52

seoul_location.sql
0.10MB

 


 

 

 

 

LocationVO.java

package com.sist.dao;

/*
	NO      NOT NULL NUMBER         
	TITLE   NOT NULL VARCHAR2(200)  
	POSTER  NOT NULL VARCHAR2(500)  
	MSG     NOT NULL VARCHAR2(4000) 
	ADDRESS NOT NULL VARCHAR2(300)
 */

public class LocationVO {

	private int no;
	private String title, poster, msg, address;
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getPoster() {
		return poster;
	}
	public void setPoster(String poster) {
		this.poster = poster;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

 

 

LocaionDAO.java

package com.sist.dao;

import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class LocationDAO {

	private Connection conn;
	private PreparedStatement ps;
	
	// 미리 생성한 Connection의 주소를 얻어온다
	public void getConnection()
	{
		try
		{
			Context init= new InitialContext();
			Context c= (Context)init.lookup("java://comp/env");
			DataSource ds= (DataSource)c.lookup("jdbc/oracle");
			conn= ds.getConnection();
		} catch(Exception ex) {}
	}
	
	// 사용 후에 재사용을 위해서 반환한다
	public void disConnection()
	{
		try
		{
			if(ps!=null) ps.close();
			if(conn!=null) conn.close();
			
		}catch(Exception ex) {}
	}
	
	public List<LocationVO> locationListData(int page)
	{
		List<LocationVO> list= new ArrayList<>();
		
		try
		{
			getConnection();
			
			// 페이지 나누기
			String sql= "SELECT no, title, poster, num "
					+ "FROM (SELECT no, title, poster, rownum as num "
					+ "FROM (SELECT no, title, poster "
					+ "FROM seoul_location ORDER BY no ASC)) "
					+ "WHERE num BETWEEN ? AND ?";
						
			ps= conn.prepareStatement(sql);
			int start= (page*12)-11;
			int end= page*12;
			
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			ResultSet rs= ps.executeQuery();
			
			while(rs.next())
			{
				LocationVO vo= new LocationVO();
				
				vo.setNo(rs.getInt(1));
				vo.setTitle(rs.getString(2));
				vo.setPoster(rs.getString(3));
				vo.setPoster(rs.getString(3));
				
				list.add(vo);
			}
			rs.close();
			
		}
		catch(Exception ex) 
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return list;
	
	}
	
	// 총 페이지 구하기
	public int locationTotalPage()
	{
		int total= 0;
		
		try
		{
			getConnection();
			String sql= "SELECT CEIL(COUNT(*)/12.0) FROM seoul_location";
			ps= conn.prepareStatement(sql);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			total= rs.getInt(1);
			
			rs.close();
		} 
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return total;
		
	}
	
	
	// 상세보기
	public LocationVO locationDetailData(int no)
	{
		LocationVO vo= new LocationVO();
		
		try
		{
			getConnection();
			
			String sql= "SELECT no, title, poster, msg, address "
						+ "FROM seoul_location WHERE no=?";
			
			ps= conn.prepareStatement(sql);
			ps.setInt(1, no);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			vo.setNo(rs.getInt(1));
			vo.setTitle(rs.getString(2));
			vo.setPoster(rs.getString(3));
			vo.setMsg(rs.getString(4));
			vo.setAddress(rs.getString(5));
			
			rs.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return vo;
	}
}

 

 

SeoulModel.java

package com.sist.model;

import javax.servlet.http.HttpServletRequest;
import com.sist.dao.*;
import java.util.*;

public class SeoulModel {

	public void locationListData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String strPage= request.getParameter("page");
		
		if(strPage == null)
			strPage = "1";
		
		int curPage= Integer.parseInt(strPage);	
		List<LocationVO> list= dao.locationListData(curPage);
		
		// 총 페이지
		int totalpage= dao.locationTotalPage();
		
		final int BLOCK= 5;
		int startPage= ((curPage-1) / BLOCK*BLOCK)+1;
		int endPage= ((curPage-1) / BLOCK*BLOCK)+BLOCK;
		
		// [1]...[5] curpage가 1-5일 때까지 5페이지 유지
		// [6]...[10] startPage= 6, endPage=10 -> curpage = 6-10
		if(endPage > totalpage)
			endPage = totalpage;
		
		request.setAttribute("curpage", curPage);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("startPage", startPage);
		request.setAttribute("endPage", endPage);
		request.setAttribute("list", list);
		
	}
	
	public void locationDetailData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String no= request.getParameter("no");
		LocationVO vo= dao.locationDetailData(Integer.parseInt(no));
		request.setAttribute("vo", vo);
	}
}

 

 


 

location.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*, com.sist.dao.*"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<jsp:useBean id="model" class="com.sist.model.SeoulModel" />

<% 
	model.locationListData(request); // Controller
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.container{
	margin-top: 50px;
}
</style>
</head>
<body>
	<!-- 이미지 출력 -->
	<div class="container">
		<div class="row">
			<c:forEach var="vo" items="${list }">
				<div class="col-md-3">
					<div class="thumnail">
						<a href="detail.jsp?no=${vo.no }">
							<img src="${vo.poster }" alt="Lights" style="width:250px; height: 200px">
							<div class="caption">
								<p>${vo.title }</p>
							</div>
						</a>
					</div>
				</div>
			</c:forEach>
		</div>
		
		<!-- 페이지 출력 -->
		<div class="row">
			<div class="text-center">
				<ul class="pagination">
					
					<c:if test="${startPage>1 }">
						<li><a href="location.jsp?page=${startPage-1 }">&lt;</a></li>
					</c:if>
					
					<c:forEach var="i" begin="${startPage }" end="${endPage }">
						<li ${i==curpage?"class=active":"" }><a href="location.jsp?page=${i }">${i }</a></li>
					</c:forEach>
					
					<c:if test="${endPage<totalpage }">
						<li><a href="location.jsp?page=${endPage+1 }">&gt;</a></li>
					</c:if>
					
				</ul>
			</div>
		</div>		
	</div>
</body>
</html>

 

 

 

 

 

detail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.sist.dao.*"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="model" class="com.sist.model.SeoulModel" />

<% 
	model.locationDetailData(request);
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.container{
	margin-top: 50px;
}
.row {
	margin: 0px auto;
	width: 700px;
}

</style>

</head>
<body>
	<div class="container">
		<div class="row">
			<table class="table">
				<tr>
					<td class="text-center"><h3>${vo.title }</h3></td>
				</tr>
				<tr>
					<td class="text-center">
						<img src="${vo.poster }" style="width:100%">
					</td>
				</tr>
				<tr>
					<td>
					${vo.msg }
					</td>
				</tr>
				<tr>
					<td>
					${vo.address }
					</td>
				</tr>
			</table>
		</div>
	</div>
</body>
</html>

 

 

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.container{
	margin-top: 50px;
}
.row{
	margin: 0px auto;
	width: 700px;
}
</style>
<script type="text/javascript">

function locationLink()
{
	location.href="location.jsp";	
}

let natureLink=()=>{
	
	location.href="nature.jsp";
}

let foodLink=()=>{
	
	location.href="../food/category.jsp";
}
</script>
</head>
<body>
	<div class="container">
		<div class="row">
			<div="text-center">
				<input type="button" id="locBtn" value="명소" class="btn btn-lg btn-success" onclick="locationLink()">
				<input type="button" id="natBtn" value="자연" class="btn btn-lg btn-info" onclick="natureLink()">
				<input type="button" id="foodBtn" value="맛집" class="btn btn-lg btn-danger" onclick="foodLink()">
			</div>			
		</div>
	</div>
</body>
</html>

 

명소 클릭시 location.jsp으로 파싱

자연 클릭시 nature.jsp으로 파싱

맛집 클릭시 food.jsp으로 파싱

 

 

 



Nature

**기존에 생성한 파일에 nature용 문장만 추가한다.

 

 

seoul_nature.sql
0.04MB

 

 


 

LocationVO 기존 파일 사용.

 

 

LocationDAO.java

package com.sist.dao;

import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class LocationDAO {

	private Connection conn;
	private PreparedStatement ps;
	
	// 미리 생성한 Connection의 주소를 얻어온다
	public void getConnection()
	{
		try
		{
			Context init= new InitialContext();
			Context c= (Context)init.lookup("java://comp/env");
			DataSource ds= (DataSource)c.lookup("jdbc/oracle");
			conn= ds.getConnection();
		} catch(Exception ex) {}
	}
	
	// 사용 후에 재사용을 위해서 반환한다
	public void disConnection()
	{
		try
		{
			if(ps!=null) ps.close();
			if(conn!=null) conn.close();
			
		}catch(Exception ex) {}
	}
	
	public List<LocationVO> locationListData(int page)
	{
		List<LocationVO> list= new ArrayList<>();
		
		try
		{
			getConnection();
			
			// 페이지 나누기
			String sql= "SELECT no, title, poster, num "
					+ "FROM (SELECT no, title, poster, rownum as num "
					+ "FROM (SELECT no, title, poster "
					+ "FROM seoul_location ORDER BY no ASC)) "
					+ "WHERE num BETWEEN ? AND ?";
						
			ps= conn.prepareStatement(sql);
			int start= (page*12)-11;
			int end= page*12;
			
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			ResultSet rs= ps.executeQuery();
			
			while(rs.next())
			{
				LocationVO vo= new LocationVO();
				
				vo.setNo(rs.getInt(1));
				vo.setTitle(rs.getString(2));
				vo.setPoster(rs.getString(3));
				vo.setPoster(rs.getString(3));
				
				list.add(vo);
			}
			rs.close();
			
		}
		catch(Exception ex) 
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return list;
	
	}
	
	// 총 페이지 구하기
	public int locationTotalPage()
	{
		int total= 0;
		
		try
		{
			getConnection();
			String sql= "SELECT CEIL(COUNT(*)/12.0) FROM seoul_location";
			ps= conn.prepareStatement(sql);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			total= rs.getInt(1);
			
			rs.close();
		} 
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return total;
		
	}
	
	
	// 상세보기
	public LocationVO locationDetailData(int no)
	{
		LocationVO vo= new LocationVO();
		
		try
		{
			getConnection();
			
			String sql= "SELECT no, title, poster, msg, address "
						+ "FROM seoul_location WHERE no=?";
			
			ps= conn.prepareStatement(sql);
			ps.setInt(1, no);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			vo.setNo(rs.getInt(1));
			vo.setTitle(rs.getString(2));
			vo.setPoster(rs.getString(3));
			vo.setMsg(rs.getString(4));
			vo.setAddress(rs.getString(5));
			
			rs.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return vo;
	}
	 
	//////////////////////////// NATURE ///////////////////////////////////
	
	public List<LocationVO> natureListData(int page)
	{
		List<LocationVO> list= new ArrayList<>();
		
		try
		{
			getConnection();
			
			// 페이지 나누기
			String sql= "SELECT no, title, poster, num "
					+ "FROM (SELECT no, title, poster, rownum as num "
					+ "FROM (SELECT no, title, poster "
					+ "FROM seoul_nature ORDER BY no ASC)) "
					+ "WHERE num BETWEEN ? AND ?";
						
			ps= conn.prepareStatement(sql);
			int start= (page*12)-11;
			int end= page*12;
			
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			ResultSet rs= ps.executeQuery();
			
			while(rs.next())
			{
				LocationVO vo= new LocationVO();
				
				vo.setNo(rs.getInt(1));
				vo.setTitle(rs.getString(2));
				vo.setPoster(rs.getString(3));
				vo.setPoster(rs.getString(3));
				
				list.add(vo);
			}
			rs.close();
			
		}
		catch(Exception ex) 
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return list;
	
	}
	
	// 총 페이지 구하기
	public int natureTotalPage()
	{
		int total= 0;
		
		try
		{
			getConnection();
			String sql= "SELECT CEIL(COUNT(*)/12.0) FROM seoul_nature";
			ps= conn.prepareStatement(sql);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			total= rs.getInt(1);
			
			rs.close();
		} 
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return total;
		
	}
	
	
	// 상세보기
	public LocationVO natureDetailData(int no)
	{
		LocationVO vo= new LocationVO();
		
		try
		{
			getConnection();
			
			String sql= "SELECT no, title, poster, msg, address "
						+ "FROM seoul_nature WHERE no=?";
			
			ps= conn.prepareStatement(sql);
			ps.setInt(1, no);
			
			ResultSet rs= ps.executeQuery();
			rs.next();
			vo.setNo(rs.getInt(1));
			vo.setTitle(rs.getString(2));
			vo.setPoster(rs.getString(3));
			vo.setMsg(rs.getString(4));
			vo.setAddress(rs.getString(5));
			
			rs.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			disConnection();
		}
		return vo;
	}
}

 

메서드를 복붙해서 메서드 이름만 nature에 맞게 바꾼다.

 

 

 

SeoulModel.java

package com.sist.model;

import javax.servlet.http.HttpServletRequest;
import com.sist.dao.*;
import java.util.*;

public class SeoulModel {

	public void locationListData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String strPage= request.getParameter("page");
		
		if(strPage == null)
			strPage = "1";
		
		int curPage= Integer.parseInt(strPage);	
		List<LocationVO> list= dao.locationListData(curPage);
		
		// 총 페이지
		int totalpage= dao.locationTotalPage();
		
		final int BLOCK= 5;
		int startPage= ((curPage-1) / BLOCK*BLOCK)+1;
		int endPage= ((curPage-1) / BLOCK*BLOCK)+BLOCK;
		
		// [1]...[5] curpage가 1-5일 때까지 5페이지 유지
		// [6]...[10] startPage= 6, endPage=10 -> curpage = 6-10
		if(endPage > totalpage)
			endPage = totalpage;
		
		request.setAttribute("curpage", curPage);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("startPage", startPage);
		request.setAttribute("endPage", endPage);
		request.setAttribute("list", list);
		
	}
	
	public void locationDetailData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String no= request.getParameter("no");
		LocationVO vo= dao.locationDetailData(Integer.parseInt(no));
		request.setAttribute("vo", vo);
	}
	
	///////////////////// NATURE //////////////////////////////////
	
	
	public void natureListData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String strPage= request.getParameter("page");
		
		if(strPage == null)
			strPage = "1";
		
		int curPage= Integer.parseInt(strPage);	
		List<LocationVO> list= dao.natureListData(curPage);
		
		// 총 페이지
		int totalpage= dao.locationTotalPage();
		
		final int BLOCK= 5;
		int startPage= ((curPage-1) / BLOCK*BLOCK)+1;
		int endPage= ((curPage-1) / BLOCK*BLOCK)+BLOCK;
		
		// [1]...[5] curpage가 1-5일 때까지 5페이지 유지
		// [6]...[10] startPage= 6, endPage=10 -> curpage = 6-10
		if(endPage > totalpage)
			endPage = totalpage;
		
		request.setAttribute("curpage", curPage);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("startPage", startPage);
		request.setAttribute("endPage", endPage);
		request.setAttribute("list", list);
		
	}
	
	public void natureDetailData(HttpServletRequest request)
	{
		LocationDAO dao= new LocationDAO();
		String no= request.getParameter("no");
		LocationVO vo= dao.locationDetailData(Integer.parseInt(no));
		request.setAttribute("vo", vo);
	}
	
}

 

 

 

 

nature.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*, com.sist.dao.*"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<jsp:useBean id="model" class="com.sist.model.SeoulModel" />

<% 
	model.natureListData(request); // Controller
%>

<% 
	LocationDAO dao= new LocationDAO();
	String strPage= request.getParameter("page");
	
	if(strPage == null)
		strPage = "1";
	
	int curpage= Integer.parseInt(strPage);
	
	List<LocationVO> list= dao.natureListData(curpage);
	request.setAttribute("list", list);

%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.container{
	margin-top: 50px;
}
</style>
</head>
<body>
	<!-- 이미지 출력 -->
	<div class="container">
		<div class="row">
			<c:forEach var="vo" items="${list }">
				<div class="col-md-3">
					<div class="thumnail">
						<a href="detail.jsp?no=${vo.no }">
							<img src="${vo.poster }" alt="Lights" style="width:250px; height: 200px">
							<div class="caption">
								<p>${vo.title }</p>
							</div>
						</a>
					</div>
				</div>
			</c:forEach>
		</div>
		
		<!-- 페이지 출력 -->
		<div class="row">
			<div class="text-center">
				<ul class="pagination">
					
					<c:if test="${startPage>1 }">
						<li><a href="nature.jsp?page=${startPage-1 }">&lt;</a></li>
					</c:if>
					
					<c:forEach var="i" begin="${startPage }" end="${endPage }">
						<li ${i==curpage?"class=active":"" }><a href="nature.jsp?page=${i }">${i }</a></li>
					</c:forEach>
					
					<c:if test="${endPage<totalpage }">
						<li><a href="nature.jsp?page=${endPage+1 }">&gt;</a></li>
					</c:if>
					
				</ul>
			</div>
		</div>		
	</div>
</body>
</html>

 

location.jsp 복붙해서 메서드만 locationDAO의 nature 메서드로 바꾼다.

 

 

 

 

 

**detail은 미구현