상세 컨텐츠

본문 제목

JSP 공부 <2> - JSTL , EL 연습

JSP

by oimb 2018. 9. 7. 17:43

본문

JSP를 공부하던 도중 JSTL 과 EL의 쓰임이 아직 익숙치 않아서 적응하기 위해 연습을 하고있다 


오늘 연습용 코드를 몇개 올릴까 한다.


1.    forEach 연습


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        for (int i = 0; i < 10; i++) {
    %>
            <option><%=%></option>    
    <%
        }
    %>
    이를 JSTL 과 EL 활용
    <br>
    <c:set var="i" value="0"/>
    <c:forEach var="i" begin="1" end="10">
        ${i-1}<br>    
    </c:forEach>    
</body>
</html>
cs


2. JSTL을 이용한 Map 연습


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    Map<String,String>  map = new HashMap<>();    
%>
    <c:set var="map" value="<%=map%>"></c:set>
    <c:set target="${map}" property="his" value="story"/>
    <c:out value="${map['his']}"/> == ${map.his}
</body>
</html>
cs


3. EL을 이용한 객체 호출


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    String str = "ABCDEFGHIJ";
    out.write(str.substring(3));
%>
<br>
<c:set var="str" value="ABCDEFGHIJ"/>
${str.substring(3)}
</body>
</html>
cs


4. JSTL을 이용한 Entry 호출


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
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        ArrayList<HashMap<StringString>> list = new ArrayList<HashMap<StringString>>();
 
        HashMap<StringString> map = new HashMap<StringString>();
 
        map.put("dong1""정왕1동");
        map.put("dong2""정왕2동");
        map.put("dong3""정왕3동");
 
        HashMap<StringString> map2 = new HashMap<StringString>();
 
        map2.put("dong4""정왕4동");
        map2.put("dong5""마전동");
        map2.put("dong6""정왕6동");
 
        list.add(map);
        list.add(map2);
    
        request.setAttribute("list", list);
    %>
    <br>
    <c:forEach var="list" items="${list}" varStatus="stat">
        <br>
        <c:forEach var="entry" items="${list}">
            ${entry.key}
        </c:forEach>
        <br>
        <c:forEach var="entry" items="${list}">
            ${entry.value}
        </c:forEach>
        <br>
    </c:forEach>
</body>
</html>
cs

5. JSTL을 이용한 해당 value 찾기


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 import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        ArrayList<HashMap<StringString>> list = new ArrayList<HashMap<StringString>>();
 
        HashMap<StringString> map = new HashMap<StringString>();
 
        map.put("dong1""정왕1동");
        map.put("dong2""정왕2동");
        map.put("dong3""정왕3동");
 
        HashMap<StringString> map2 = new HashMap<StringString>();
 
        map2.put("dong4""정왕4동");
        map2.put("dong5""마전동");
        map2.put("dong6""정왕6동");
 
        list.add(map);
        list.add(map2);
 
        request.setAttribute("list", list);
    %>
    <br>
    <c:forEach var="list" items="${list}" varStatus="stat">
        <c:forEach var="entry" items="${list}">
            <c:if test="${entry.value eq '마전동' }">
                찾았다<br>
                ${"key ="+= entry.key +="   value =" += entry.value }
            </c:if>
        </c:forEach>
    </c:forEach>
</body>
</html>
cs



6. JSTL을 이용한 해당 value 여러개 찾기 


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
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        ArrayList<HashMap<StringString>> list = new ArrayList<HashMap<StringString>>();
 
        HashMap<StringString> map = new HashMap<StringString>();
 
        map.put("dong1""정왕1동");
        map.put("dong2""정왕2동");
        map.put("dong3""정왕3동");
 
        HashMap<StringString> map2 = new HashMap<StringString>();
 
        map2.put("dong4""정왕4동");
        map2.put("dong5""마전동");
        map2.put("dong6""정왕6동");
 
        list.add(map);
        list.add(map2);
 
        request.setAttribute("list", list);
    %>
    <br>
    <c:forEach var="list" items="${list}" varStatus="stat">
        <c:forEach var="entry" items="${list}">
            <c:choose>
                <c:when test="${entry.value eq '정왕4동' }">
                    정왕 4동 찾았다 <br>
                </c:when>
                <c:when test="${entry.value eq '마전동' }">
                    마전동 찾았다    <br>
                </c:when>
            </c:choose>
        </c:forEach>
    </c:forEach>
</body>
</html>
cs


관련글 더보기