티스토리 뷰

BackEnd/JSP

[JSP] JSTL, taglib

best 2016. 2. 5. 15:26

1. JSTL

- JSTL은 변수, 조건문, 반복문 등 자바로직을 대체해준다.

- jsp 안에서 자바로직을 쓰지 않게 함으로써 소스에 통일성을 주며, 유지보수가 편해진다.

- 이를 쓰기 위해서는 taglib을 디렉티브로 등록해야한다.

<% taglib prefix = "c" uri=http://java.sun.com/jsp/jstl/core %> 이를 등록해준다.

 

2. forEach , if, set 사용 방법

  • forEach - 반복문

<c:forEach var="subj" items="${ subjects }" > </c:forEach>

이는 자바에서 For( String s: subjects ) 랑 비슷한 의미이다.

  • if - 조건문

<c:if test="${ not empty subjects }">

만약 subjects가 비어있지 않다면 이라는 조건문이다.

  • set

<c:set var="iii" value="12345"/>

<c:set var="iii" value="${iii + 1}"/>

 

VO , Servlet, jsp 파일을 연동하여 리스트 출력

실습1 - ArticleVO.java

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
package com.ktds.jmj.bbs.vo;
 
public class ArticleVO {
    
    private int articleNumber;
    private String articleName;
    private int hitCount;
    
    
    public ArticleVO(int articleNumber, String articleName, int hitCount) {
    
        this.articleNumber = articleNumber;
        this.articleName = articleName;
        this.hitCount = hitCount;
    }
 
 
    public int getArticleNumber() {
        return articleNumber;
    }
 
 
    public void setArticleNumber(int articleNumber) {
        this.articleNumber = articleNumber;
    }
 
 
    public String getArticleName() {
        return articleName;
    }
 
 
    public void setArticleName(String articleName) {
        this.articleName = articleName;
    }
 
 
    public int getHitCount() {
        return hitCount;
    }
 
 
    public void setHitCount(int hitCount) {
        this.hitCount = hitCount;
    }
    
}
 
cs

 

실습2 - BBSServlet.java

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
50
51
52
53
54
55
56
57
58
package com.ktds.jmj.bbs.web;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.ktds.jmj.bbs.vo.ArticleVO;
 
/**
 * Servlet implementation class BBSServlet
 */
public class BBSServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BBSServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        List<ArticleVO> articleList = new ArrayList<ArticleVO>();
        ArticleVO articleVO = null;
        
        for ( int i = 0; i < 10; i++) {
            articleVO = new ArticleVO( (i + 1), "제목" + (i + 1),  new Random().nextInt(100));
            articleList.add(articleVO);
        }
        
        request.setAttribute("articleList", articleList); //articleList라는 인스턴스를 articleList라는 키로 보낸다.
        
        RequestDispatcher rd = request.getRequestDispatcher("/view/jsp/bbs/list.jsp");
        rd.forward(request, response);
        
    }
 
}
 
cs

 

실습3 - 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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
안녕하세요....<br/>
 
총 ${ articleList.size() } 개의 게시글이 등록되어 있습니다. <br/>
 
<table border = "1">
 
    <tr>
        <th> 글 번호 </th>
        <th> 제목 </th>
        <th> 조회수 </th>
    </tr>
<c:if test"${ not empty articleList }">    
    <c:forEach var="article" items="${ articleList }">
        <tr>
            <td>${ article.articleNumber }</td> <!-- 내부적으로 getArticleNumber을 호출한다. -->
            <td>${ article.articleName }</td>
            <td>${ article.hitCount }</td>
        </tr>
    </c:forEach>
</c:if>
<c:set var="iii" value="124132532524"/>
${ iii }
<c:set var="iii" value="${iii+1 }"/>
${ iii }
<c:if test = "${ empty articleList }">    
    <tr>
        <td colspan="3"> 데이터가 없습니다. </td>
    </tr>
</c:if>        
 
</table>
 
</body>
</html>
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/01   »
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
글 보관함