BackEnd/JSP

[JSP] list를 이용해서 table 만들기 , import

best 2016. 2. 2. 19:13

1. list에 넣을 정보 VO.java생성

src 아래에 VO.java를 생성하여 list에 넣을 정보를 생성한다.

 

 

2. import 하기

  • import가 필요할 때

필요한 부분에 커서를 올려두고 ctrl+shift+M 을 눌러 자동으로 import 시킨다.

  • 파일을 import 할때

보통 import와 같은 형식으로 import

<%@page import="com.ktds.jmj.DramaVO"%>

 

 

3. for문을 이용해 list를 테이블에 넣는다.

<table border="1">

<tr>

<th>방송사 </th>

<th> 드라마제목 </th>

</tr>

<%

for ( DramaVO drama : dramas ) {

%>

<tr>

<td><%= drama.getBroadcastStation() %> </td>

<td><%= drama.getDramaName() %> </td>

</tr>

<% } %>

</table>

 

 

 

DramaVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DramaVO {
        
        private String dramaName;
        private String broadcastStation;
        
        public String getDramaName() {
            return dramaName;
        }
        public void setDramaName(String dramaName) {
            this.dramaName = dramaName;
        }
        public String getBroadcastStation() {
            return broadcastStation;
        }
        public void setBroadcastStation(String broadcastStation) {
            this.broadcastStation = broadcastStation;
        }
        
}
 
cs

 

 

list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.List"%> <!-- 선언부 밑에 위치해야한다. -->
<%@page import="java.util.ArrayList"%>
<%@page import="com.ktds.jmj.DramaVO"%>
<%-- <%@include file="DramaVO.jsp" %>--%>
 
<%
    List<String> news = new ArrayList<String>(); 
    news.add("MBC 납량특집 드라마 M");
    news.add("MBC 납량특집 드라마 거미");
    news.add("KBS 납량특집 전설의 고향");
    news.add("SBS 토요 미스테리");
    DramaVO vo = null;
    
    List<DramaVO> dramas = new ArrayList<DramaVO>();
    forint i = 0; i < 4; i++ ) {
        vo = new DramaVO();
        vo.setBroadcastStation("MBC");
        vo.setDramaName("M" + i);
        dramas.add(vo);
    }
%>
 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table border="1">
        <tr>
            <th> 방송사 </th>
            <th> 드라마 제목 </th>
        </tr>
<%
    for( DramaVO drama : dramas ){
%>
        <tr>
            <td><%= drama.getBroadcastStation() %></td>
            <td><%= drama.getDramaName() %></td>
        </tr>
<% } %>
    </table>
 
</body>
</html>
cs