본문 바로가기

Web/기타

[JSTL] JSTL - if, choose, forEach, import

if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<c:set var="n" scope="request" value="10" />
 
<!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">
    </head>
    <body>
        <c:if test="${n == 0}">
            n은 0과 같습니다.
        </c:if>
 
        <c:if test="${n == 10}">
            n은 10과 같습니다.
        </c:if>
    </body>
</html>
cs



choose

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<c:set var="score" scope="request" value="83" />
 
<!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">
    </head>
    <body>
        <c:choose>
            <c:when test="${score >=90 }">
            A학점입니다.
            </c:when>
            <c:when test="${score >=80 }">
            B학점입니다.
            </c:when>
            <c:when test="${score >=70 }">
            C학점입니다.
            </c:when>
            <c:when test="${score >=60 }">
            D학점입니다.
            </c:when>
            <c:otherwise>
            F학점입니다.
            </c:otherwise>            
        </c:choose>
    </body>
</html>
cs



forEach

배열 및 Collection에 저장된 요소를 차례대로 처리한다.

1
2
3
4
5
<c:forEach var="변수" items="아이템" [begin="시작번호"] [end="끝번호"]>
...
${변수}
...
</c:forEach>
cs


var - EL에서 사용될 변수명

items - 배열, List, Iterator, Enumeration, Map 등의 Collection

begin - items에 지정한 목록에서 값을 읽어올 인덱스의 시작값

end - items에 지정한 목록에서 값을 읽어올 인덱스의 끝값


아이템이 Map인 경우 변수에 저장되는 객체는 Map.Entry이다. 

따라서, 변수값을 사용할 때는 ${변수.key}와 ${변수.value}를 사용해서 맵에 저장된 항목의 <키, 값> 매핑에 접근할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.*" %>
<%
    List<String> list = new ArrayList<>();
    list.add("hello");
    list.add("world");
    list.add("!!!");
    request.setAttribute("list", list);
%>
 
<!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">
    </head>
    <body>
        <c:forEach items="${list}" var="item">
        ${item } <br>
        </c:forEach>
    </body>
</html>
cs



import

지정한 URL에 연결하여 결과를 지정한 변수에 저장한다.

1
2
3
<c:import url="URL" charEncoding="캐릭터 인코딩" var="변수명" scope="범위">
    <c:param name="파라미터 이름" value="파라미터 값"/>
</c:import>
cs


url - 결과를 읽어올 URL

charEncoding - 읽어온 결과를 저장할 때 사용할 캐릭터 인코딩

var - 읽어온 결과를 저장할 변수명

scope - 변수를 저장할 영역

<c:param>태그는 url 속성에 지정한 사이트에 연결할 때 전송할 파라미터를 입력한다.


- jstlValue.jsp

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


- jstl.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<c:import url="http://localhost:9090/firstWeb/jstlValue.jsp" var="urlValue" scope="request" />
 
<!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">
    </head>
    <body>
        읽어들인 값 : ${urlValue}
    </body>
</html>
cs










참고자료

https://www.edwith.org

'Web > 기타' 카테고리의 다른 글

[JSTL] JSTL - redirect, out  (0) 2018.12.27
[JSTL] JSTL - set, remove  (0) 2018.12.27
[JSTL] JSTL 개요  (0) 2018.12.27
[EL] EL(2)  (0) 2018.12.26
[EL] EL(1)  (0) 2018.12.26