개발자
[JSP 3일차]태그 라이브러리 /< c:choose>/<c:forEach>/forTokens 본문
태그 라이브러리
<%-- === JSTL(JSP Standard Tag Library) 사용하기 === --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
< c:choose>
<c:if>는 else 가 없지만 <c:choose>는 있다
<c:choose>
<c:when>
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
1. 실행페이지 .jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력한 주민번호를 가지고 성별 파악하기</title>
</head>
<body>
<form action="03_choose_result_02.jsp">
주민번호 : <input type="text" name="jubun" />
<input type="submit" value="확인" />
<input type="reset" value="취소" />
</form>
</body>
</html>
|
cs |
<form action="03_choose_result_02.jsp">
2. 03_choose_result_02.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
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- ==== JSTL(JSP Standard Tag Library) 사용하기 ==== --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>choose 를 사용하여 전송되어져온 주민번호를 가지고 성별을 파악한 결과물 출력하기</title>
</head>
<body>
<c:set var="jubun" value="${param.jubun}" />
<c:set var="gender_no" value="${fn:substring(jubun,6,7) }"/>
<c:set var="jubun_length" value="${fn:length(jubun) }"/>
주민번호 :${jubun} <br/>
성별을 나타내어주는 숫자 : ${gender_no} <br><br>
<c:if test="${jubun_length eq 0}">
<%--eq 대신 ==도 가능 --%>
<span style="color: red">주민번호를 입력하지 않으셨습니다.!!</span> </br>
</c:if>
<c:if test="${jubun_length ne 0 and jubun_length ne 13 }">
<%--ne 대신 != 도 가능 // and 대신 && 도 가능 --%>
<span style="color: red">주민번호의 길이가 맞지 않습니다.!!</span> </br>
</c:if>
<c:if test="${jubun_length eq 13}">
<c:choose>
<c:when test="${gender_no eq '1'}">
<%--test는 조건을 말한다 --%>
1900년대생 남자입니다.
</c:when>
<c:when test="${gender_no eq '2'}">
1900년대생 여자입니다.
</c:when>
<c:when test="${gender_no eq '3'}">
2000년대생 남자입니다.
</c:when>
<c:when test="${gender_no eq '4'}">
2000년대생 여자입니다.
</c:when>
<c:otherwise>
<%-- else --%>
주민번호 7번째 자리의 값이 1부터 4가 아니군요!!
</c:otherwise>
</c:choose>
</c:if>
</body>
</html>
|
cs |
<c:forEach>
<c:forEach var="i" begin="1" end="6">
: 1부터 6까지 6번 반복 +1씩 -
<연습 1>
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"%>
<%-- ==== JSTL(JSP Standard Tag Library) 사용하기 ==== --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>반복문 연습하기</title>
</head>
<body>
<c:forEach var="i" begin="1" end="6">
<%-- 1부터 6까지 6번 반복 +1씩 --%>
<h${i}>반복문 연습</h${i}>
</c:forEach>
</body>
</html>
|
cs |
<연습2>
view 단에서 02.jsp에서 하는것
1.배열 꺼내기 :
<c:forEach var="friend_name" items="${requestScope.arr_friend_name}">
items="${}"에 들어오는 것은 배열 또는 List 이다.(fortokens는 문자열)
자동적으로 배열길이만큼 반복하여 items의 값을 friend_name에 하나씩 넣어준다
돌린건 01 보여주는건 02.jsp 포워드 시켰기 때문에
<li>${friend_name}</li>
2.List 꺼내기
<c:forEach var="psdto" items="${personList}">
requestScope 생략가능 ul은 list만큼 돌아감 dto형식의 배열리스트니까 psdto로 작명
<ul>
<li>성명 : ${psdto.irum}</li>
irum은 필드네임이 아니라 get다음에오는 Irum 이다 앞에 무조건 소문자라서 irum이 된다. getIrum 인것
<li>학력 : ${psdto.school}</li>
<li>색상 : ${psdto.color}</li>
<li>음식 : ${psdto.strFood}</li>
</ul>
</c:forEach>
1.05_forEach_Array_List_execute_01.jspa
: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
50
51
52
53
54
55
56
57
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*, chap03.PersonDTO" %>
<%
//배열 써보기
String[] arr_friend_name = {"임선우","장진영","조상운","진민지","진혜린","황광빈"};
request.setAttribute("arr_friend_name", arr_friend_name);
///////////////////////////////////////////////////////////////
//List 로 써보기 DTO 사용해서
List<PersonDTO> personList = new ArrayList<>();
PersonDTO person1 = new PersonDTO();
person1.setIrum("김태희");
person1.setSchool("대졸");
person1.setColor("red");
person1.setFood("쵸콜릿,마이쥬,월드콘".split(","));
// . 이면 split할때 \\.으로 하지만 ,만큼은 \\ 생략가능
PersonDTO person2 = new PersonDTO();
person2.setIrum("아이유");
person2.setSchool("초대졸");
person2.setColor("blue");
person2.setFood("육회비빔밥,광어회,조개구이,참이슬".split("\\,"));
PersonDTO person3 = new PersonDTO();
person3.setIrum("박보영");
person3.setSchool("대학원졸");
person3.setColor("green");
person3.setFood("라면,떡볶이,순대,피자".split("\\,"));
personList.add(person1);
personList.add(person2);
personList.add(person3);
request.setAttribute("personList", personList);
RequestDispatcher dispatcher = request.getRequestDispatcher("05_forEach_Array_List_view_02.jsp");
dispatcher.forward(request, response);
/*
05_forEach_Array_List_view_02.jsp 파일만 request영역에 저장되어있는 etAttribute를 꺼내어 볼 수 있다.
지금은 주소에 /가없으므로 상대경로이다 (같은파일에있다)
돌린건 01 보여주는건 02.jsp의 내용물, 포워드 시켰기 때문에 주소는 01로 나온다
미리 자바를 만들고 포워드페이지는 다른사람이만들어서 결과값만 가져다 쓰면된다.
01페이지는 보여주지않기때문에 보안성이 뛰어나다.
*/
// 이페이지를 서블릿으로 보면된다. 이 jsp는 자바라고 보면된다
%>
|
cs |
2 .05_forEach_Array_List_view_02.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- === JSTL(JSP Standard Tag Library) 사용하기 === --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>친구명단 출력하기</title>
</head>
<body>
<c:if test="${empty requestScope.arr_friend_name}">
<%-- 배열값이 없다면 --%>
<div>
<span stylr="color:red;">친구명단이 없습니다.</span>
</div>
</c:if>
<c:if test="${not empty requestScope.arr_friend_name}">
<div>
<ol>
<%-- 배열 꺼내기 --%>
<c:forEach var="friend_name" items="${requestScope.arr_friend_name}">
<%--items="${}"에 들어오는 것은 배열 또는 List 이다. --%>
<%-- items에는 배열아니면 list만 들어온다 /자동적으로 배열길이만큼 반복
items의 값을 friend_name에 하나씩 넣어준다
돌린건 01 보여주는건 02.jsp 포워드 시켰기 때문에
--%>
<li>${friend_name}</li>
</c:forEach>
</ol>
</div>
</c:if>
<hr style="border : soild 1px red; margin : 20px 0;">
<c:if test="${empty requestScope.personList}">
<%-- requestScope 생략가능--%>
<div>
<span style="color : red;">회원명단이 없습니다</span>
</div>
</c:if>
<c:if test="${not empty requestScope.personList}">
<%-- 있다면--%>
<div>
<%-- List 꺼내기 --%>
<c:forEach var="psdto" items="${personList}">
<%-- requestScope 생략가능 ul list만큼 돌아감 dto형식의 배열리스트니까 psdto로 작명--%>
<ul>
<li>성명 : ${psdto.irum}</li>
<%-- 필드네임이 아니라 get다음에오는 Irum 이다 앞에 무조건 소문자라서 irum이 된다. getIrum 인것--%>
<li>학력 : ${psdto.school}</li>
<li>색상 : ${psdto.color}</li>
<li>음식 : ${psdto.strFood}</li>
</ul>
</c:forEach>
</div>
</c:if>
</body>
</html>
|
cs |
<배열과 리스트에 데이터 값을 안넣어주었다면 >
forTokens
1. jsp 이지만 실행페이지이자 서블릿
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//배열아니고 문자열이다
String friendNames_1 = "임선우,장진영,조상운,진민지,진혜린,황광빈";
String friendNames_2 = "이제훈,고수.강동원,주원/이순신";
request.setAttribute("friendNames_1", friendNames_1);
request.setAttribute("friendNames_2", friendNames_2);
RequestDispatcher dispatcher = request.getRequestDispatcher("06_forTokens_Array_List_view_02.jsp");
//특정한 view단만 보겠다
dispatcher.forward(request, response);
%>
|
cs |
2.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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%-- === JSTL(JSP Standard Tag Library) 사용하기 === --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>forTokens 를 이용하여 친구이름 출력하기</title> </head> <body> <h2>forTokens 를 이용하여 친구이름 출력하기 -1</h2> <c:if test="${requestScope.friendNames_1 == null }"> <div> <span style="color:red;">친구가 없으시군요~~^____^</span> </div> </c:if> <%-- empty로 쓰기를 권장 null은 아닌데 데이터가 없을때도 출력해주기 때문이다--%> <c:if test="${empty requestScope.friendNames_1}"> <div> <span style="color:red;">친구가 없으시군요~~^____^</span> </div> </c:if> <c:if test="${requestScope.friendNames_1 != null }"> <div> <c:forTokens var="name" items="${friendNames_1}" delims=","> <%-- forTokens 에서 items="${}"에 들어오는 것은 배열이나 List가 아니다 문자열을 , 로 잘라서 배열로 만들어준다. --%> <li>${name}</li> </c:forTokens> </div> <br> </c:if> <%-- empty로 쓰기를 권장--%> <c:if test="${not empty requestScope.friendNames_1}"> <div> <c:forTokens var="name" items="${friendNames_1}" delims=","> <%-- forTokens 에서 items="${}"에 들어오는 것은 배열이나 List가 아니다 문자열을 , 로 잘라서 배열로 만들어준다. --%> <li>${name}</li> </c:forTokens> </div> </c:if> <hr style="border:solid 1px red; margin: 5px 0;"> <%-- empty로 쓰기를 권장 null은 아닌데 데이터가 없을때도 출력해주기 때문이다--%> <c:if test="${empty requestScope.friendNames_2}"> <div> <span style="color:red;">친구가 없으시군요~~^____^</span> </div> </c:if> <%-- empty로 쓰기를 권장--%> <c:if test="${not empty requestScope.friendNames_2}"> <div> <c:forTokens var="name" items="${friendNames_2}" delims=",./"> <%-- forTokens 에서 items="${}"에 들어오는 것은 배열이나 List가 아니다 문자열을 , 또는 . 또는 /로 잘라서 배열로 만들어준다. --%> <li>${name}</li> </c:forTokens> </div> </c:if> <hr style="border: solid 1px blue; margin: 20px 0;"> <h2>split 함수를 사용하여 배열로 만든 다음 forEach를 사용하여 친구이름 출력하기 -2</h2> <c:if test="${empty requestScope.friendNames_1}"> <div> <span style="color:red;">친구가 없으시군요~~^____^</span> </div> </c:if> <c:if test="${not empty requestScope.friendNames_1}"> <c:set var="arr_friendNames_1" value="${ fn:split(requestScope.friendNames_1,',')}"/> <%--,로 쪼개주겠다 --%> <div> <ol> <c:forEach var="name" items="${arr_friendNames_1}"> <li>${name}</li> </c:forEach> </ol> </div> </c:if> <hr style="border:solid 1px red; margin: 5px 0;"> <c:if test="${empty requestScope.friendNames_2}"> <div> <span style="color:red;">친구가 없으시군요~~^____^</span> </div> </c:if> <c:if test="${not empty requestScope.friendNames_2}"> <c:set var="arr_friendNames_2" value="${ fn:split(requestScope.friendNames_2,',')}"/> <%--,로 쪼개주겠다 --%> <div> <ol> <c:forEach var="name" items="${arr_friendNames_2}"> <li>${name}</li> </c:forEach> </ol> </div> </c:if> </body> </html> | cs |
'개발자 > JSP' 카테고리의 다른 글
[JSP 5일차] Oracle과 연동2 (0) | 2022.09.02 |
---|---|
[JSP 4일차]fmt/Oracle과 연동/ (0) | 2022.09.01 |
[JSP 3일차] DTO/useBean/커스텀액션(Custom action)/taglib(<c:set>/<c:if>) (0) | 2022.08.31 |
[JSP 2일차] survelt(urlmapping)/action/include/forward/request/filter/Post만 받도록하는 서블릿/ (0) | 2022.08.30 |
[JSP 1일차]JSP란?/환경설정/현재시각/지시어(page,include)/Servlet(서블릿) 조건/jps,xml,servlet 관계 (0) | 2022.08.29 |