AngelPlayer`s Diary

※ 직접 풀이를 진행하였기 때문에, 정답에 오류가 있을 수 있습니다.

오류를 발견 시, 댓글로 남겨주시면 감사하겠습니다..!

 

※ 소스 코드는 Copy & Paste를 하면 제대로 출력되지 않아서,

최하단에 별도의 첨부파일로 제공합니다.

 

쉽게 배우는 JSP 웹 프로그래밍 (송미영 저)

 

 

 

01. 

forward 액션 태그 : 현재 페이지에서 다른 페이지로 이동 (제어 자체가 다른 페이지로 이동함)

include 액션 태그 : 현재 페이지의 특정 영역에 다른 페이지의 내용을 포함

 

 

 

02.

include 액션 태그 : 다른 페이지의 결과 내용이 포함, 레이아웃 모듈화에 사용

include 디렉티브 태그 : 단순하게 텍스트 내용이 포함됨, 문장을 포함하는데 사용

 

 

 

03. 

1. java.io.Serializable 인터페이스를 구현해야 함(implement)

2. 인수가 없는 기본 생성자가 있어야 함 -> public MemberBean() {}

3. 모든 프로퍼티(멤버 변수)는 private로 선언되고, Getter/Setter를 필요로 함

 

 

 

04. 

forward.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Action Tag</title>
</head>
<body>
	<jsp:forward page="forward_data.jsp" />
</body>
</html>

 

forward_data.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Action Tag</title>
</head>
<body>
	<%!
		String output;
	%>
	<%
		for (int i = 1; i < 10; i++) {
			output = ("5 * " + i + "=" + 5 * i);	
	%>
		<p><%=output%></p>
	<%
		}
	%>
	
</body>
</html>

 

 

 

05.

include.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Action Tag</title>
</head>
<body>
	<h4>구구단 출력하기</h4>
	<jsp:include page="include_data.jsp" flush="false" />
	
</body>
</html>

 

include_data.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Action Tag</title>
</head>
<body>
	<%! String output; %>
	
	<%
		for (int i = 1; i < 10; i++) {
			output = ("5 * " + i + "=" + 5 * i);	
	%>
		<p><%=output%></p>
	<%
		}
	%>
</body>
</html>

 

 

 

06.

 

GuGuDan.java

package ch04.com.dao;

import java.util.ArrayList;

public class GuGuDan {
	private ArrayList<String> output = new ArrayList<>();
	public GuGuDan() {
		
	}
	
	public ArrayList<String> getOutput() {
		return output;
	}

	public void setOutput(ArrayList<String> output) {
		this.output = output;
	}

	public void process() {
		for (int i = 1; i < 10; i++) {
			output.add("5 * "+ i + "=" + 5*i);
		}
	}
	
}

 

useBean.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page import = "java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h4>구구단 출력하기</h4>
	
	<jsp:useBean id="gugudan" class="ch04.com.dao.GuGuDan" scope="page"></jsp:useBean>

 	<% 
		gugudan.process();
 	
 		ArrayList<String> output = gugudan.getOutput();
		
 		for (int i=0; i<output.size(); i++) {
	%>
	
	<p><% out.println(output.get(i)); %></p>
	
	
	<%
		}
	%>
	
	
</body>
</html>

 

 

 

07.

Book.java

package ch04.com.dto;

public class Book {
	private String bookId;
	private String name;
	private Integer unitPrice;
	private String author;
	private String description;
	private String publisher;
	private String category;
	private long totalPages;
	private long unitInStock;
	private String releaseDate;
	private String condition;
	
	public Book() {
		super();
	}
	
	public Book(String bookId, String name, String author) {
		this.bookId = bookId;
		this.name = name;
		this.author = author;
	}
	
	
	
	
	

	public String getBookId() {
		return bookId;
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(Integer unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public long getUnitInStock() {
		return unitInStock;
	}

	public void setUnitInStock(long unitInStock) {
		this.unitInStock = unitInStock;
	}

	public String getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(String releaseDate) {
		this.releaseDate = releaseDate;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}
	
	
	
}

 

 

BookRepository.java

package ch04.com.dao;

import java.util.ArrayList;

import ch04.com.dto.Book;

public class BookRepository {
	ArrayList<Book> listOfBooks = new ArrayList<>();
	
	public BookRepository() {
		Book book1 = new Book("001", "HTML5+CSS3", "황재호");
		book1.setUnitPrice(15000);
		book1.setDescription("워드나 PPt 문서를 만들 수 있나요? ...");
		book1.setPublisher("한빛미디어");
		book1.setCategory("[Hello Coding]");
		book1.setUnitInStock(100);
		book1.setTotalPages(1000);
		book1.setReleaseDate("2020.02.20");
		book1.setCondition("new");
		
		
		Book book2 = new Book("002", "쉽게 배우는 자바 프로그래밍", "우종중");
		book2.setUnitPrice(27000);
		book2.setDescription("객체 지향의 핵심과 자바의 현대적 기능을 충실히 ...");
		book2.setPublisher("한빛아카데미");
		book2.setCategory("[IT 모바일]");
		book2.setUnitInStock(100);
		book2.setTotalPages(1000);
		book2.setReleaseDate("2020.02.20");
		book2.setCondition("new");
		
		
		Book book3 = new Book("003", "스프링4 입문", "하세가와 유이치, 오오노 와타루, 토키 코헤이(권은철, 전민수)");
		book3.setUnitPrice(27000);
		book3.setDescription("스프링은 단순히 사용 방법만 익히는 것보다 아키텍쳐를 어떻게 이해하고 ...");
		book3.setPublisher("한빛미디어");
		book3.setCategory("[IT 모바일]");
		book3.setUnitInStock(100);
		book3.setTotalPages(1000);
		book3.setReleaseDate("2020.02.20");
		book3.setCondition("new");
		
		listOfBooks.add(book1);
		listOfBooks.add(book2);
		listOfBooks.add(book3);
	
	}
	
	public ArrayList<Book> getAllBooks() {
		return listOfBooks;
	}
}

 

 

books.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="ch04.com.dto.Book" %>


<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
	<meta charset="UTF-8">
	<title>Insert title here</title>
</head>
<body>
	<jsp:include page="menu.jsp" />

	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">도서 목록</h1>
		</div>
	</div>
	
	<jsp:useBean id="productDAO" class="ch04.com.dao.BookRepository" scope="page" />
	<%
		ArrayList<Book> listOfBooks = productDAO.getAllBooks();
	%>
	
	<div class="container">
		<div class="row">
			<%
				for (int i = 0; i < listOfBooks.size(); i++) {
					Book book = listOfBooks.get(i);
			%>
			<div class="col-md-12">
				<h3><%=book.getCategory() + " " + book.getName()%></h3>
				<br>
				<p><%=book.getDescription()%>
				<p><%=book.getAuthor() + " | " + book.getPublisher() + " | " + book.getUnitPrice() + "원"%>
				<hr style="border-style: dotted;" />
			</div>
			<%
				}
			%>
		</div>
	</div>
	
	
	<jsp:include page="footer.jsp" />
</body>
</html>

 

 

 

src 압축파일은 src 폴더에, ch 압축파일은 WebContent에 압축 풀기 후 넣어주시면 됩니다.

 

src.zip
0.00MB
ch04.zip
0.00MB

공유하기

facebook twitter kakaoTalk kakaostory naver band