JSTL 태그 part2
2016. 11. 9. 15:43ㆍProgramming/JSP
<배운 것>
1) if문
1 2 3 | <c:if test="조건"> <p>출력 결과</p> </c:if> | cs |
2) choose문(+when, otherwise): else 조건도 설정 가능
1 2 3 4 5 6 7 8 9 10 11 | <c:choose> <c:when test="${login eq 0}"> <h3>아이디 or 비밀번호가 틀립니다</h3> </c:when> <c:when test="${login eq 1}"> <h3>로그인 성공</h3> </c:when> <c:otherwise> <h3>로그인 필요합니다</h3> </c:otherwise> </c:choose> | cs |
3) forEach(반복문)으로 구구단 출력하기
1 2 3 4 5 | <c:forEach var="i" begin="1" end="9" step="1"> <c:forEach var="j" begin="1" end="9" step="1"> ${i}x${j}=${i*j} </c:forEach> </c:forEach> | cs |
4) c:forTokens 이용해 문자열 분할 후 배열에 저장하기
5) 객체 List와 map을 forEach문으로 출력
<L02CoreControl.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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | <%@ 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>Core Control</title> </head> <body> <h1>Core 태그로 제어문과 반복문을 작성해 보자</h1> <hr> <h2>c:if문으로 로그인 처리</h2> <c:set var="login" value="1" scope="session"/> <c:set var="id" value="jspLesson" scope="session"/> <c:set var="pwd" value="1234" scope="session"/> <c:set var="admin" value="0" scope="session"/> <!-- 1이면 true, 1 아니면 false가 반환됨 --> <!--c:if의 단점: else 작성 불가 --> <c:if test="${(id eq 'jspLesson')&&(pwd eq '1234')&&(loginFlag)}"> <h3>로그인 성공</h3> </c:if> <hr> <h2>c:choose로 else if 혹은 else문을 작성해보자<c:if test="${login eq 1}" var="loginFlag" /></h2> <!-- login=0이면 '아이디 or 비밀번호가 틀립니다', login=1이면 '로그인 성공', else이면 '로그인 필요합니다'--> <c:choose> <c:when test="${login eq 0}"> <h3>아이디 or 비밀번호가 틀립니다</h3> </c:when> <c:when test="${login eq 1}"> <h3>로그인 성공</h3> </c:when> <c:otherwise> <h3>로그인 필요합니다</h3> </c:otherwise> </c:choose> <hr> <!-- when 태그 안에 다시 when/if 사용 불가 --> <!-- 반면 if문은 중첩이 자유로움 --> <!-- 제어문 많이 사용하면 속도가 느려짐 --> <h1>Core 태그의 반복문</h1> <hr> <h2>c:forEach로 반복문 작성해보자</h2> <h4>구구단 2단 출력</h4> <c:forEach var="i" begin="1" end="9" step="1"> 2x${i}=${2*i} </c:forEach> <h4>구구단 출력해보자(중첩 사용)</h4> <c:forEach var="i" begin="1" end="9" step="1"> <br> <c:forEach var="j" begin="1" end="9" step="1"> ${i}x${j}=${i*j} </c:forEach> </c:forEach> <hr> <h4>forEach의 varStatus 알아보자</h4> <%String[] array = {"사과","포도","자몽","레몬"}; %> <c:forEach items="<%=array%>" begin="0" end="<%=array.length-1%>" var="var" step="1" varStatus="status"> var(변수) : ${var} 아이템(current) : ${status.current} index : ${status.index} 실행횟수(count) : ${status.count} begin : ${status.begin} end : ${status.end} step : ${status.step} isFirst : ${status.first} isLast : ${status.last} <hr> </c:forEach> <h4>1부터 100까지 홀수의 합을 구하세요</h4> <c:set var = "sum" value="0"/> <c:forEach begin="1" end="100" var="i" step="2"> <c:set var="sum" value="${sum+i}"/> </c:forEach> <h4>1~100 홀수 합 : ${sum}</h4> <hr> <h2>c:forTokens 이용해 문자열 분할 후 배열로 저장</h2> <!-- delims는 deLimiters의 약자 --> <!-- forTokens의 변수는 forTokens 태그 내에서만 유효 --> <c:set var="alphabet" value="a/b/c/d/e/f/"/> <c:forTokens items="${alphabet}" delims="/" var="al" varStatus="status"> var(변수) : ${al} 아이템(current) : ${status.current} index : ${status.index} 실행횟수(count) : ${status.count} begin : ${status.begin} end : ${status.end} step : ${status.step} isFirst : ${status.first} isLast : ${status.last} <hr> </c:forTokens> <h2>forEach로 list에 저장된 객체를 출력해 보자</h2> <%@ page import="java.util.List, java.util.ArrayList, com.jsp.bean.Member" %> <% List<Member> memList = new ArrayList<Member>(); Member mem = new Member(); mem.setId("ichi"); mem.setName("이치"); mem.setPwd("1234"); mem.setEmail("ichi@naver.com"); memList.add(0, mem); Member mem2 = new Member(); mem2.setId("niko"); mem2.setName("니코"); mem2.setPwd("1234"); mem2.setEmail("nico@naver.com"); memList.add(1, mem2); Member mem3 = new Member(); mem3.setId("sanji"); mem3.setName("산지"); mem3.setPwd("1234"); mem3.setEmail("sanji@naver.com"); memList.add(2, mem3); pageContext.setAttribute("memList", memList); %> <c:forEach items="<%=memList%>" begin="0" end="<%=memList.size()-1%>" var="list" step="1" varStatus="status"> status.index : ${status.index} id : ${list.id} name : ${list.name} pwd : ${list.pwd} email : ${list.email} <hr> </c:forEach> <% java.util.HashMap<String, Object> map = new java.util.HashMap<String, Object>(); map.put("id", "jspLesson"); map.put("name", "walker"); map.put("pwd", 1234); map.put("admin", '1'); map.put("indate", new java.util.Date()); %> <c:forEach items="<%=map%>" var="i" varStatus="status"> ${i.key}=${i.value} </c:forEach> </body> </html> | cs |
<배운 것>
1) URLEncoder 통해 파라미터를 퍼센트화
<%
String name = URLEncoder.encode("이치", "UTF-8");
String id = URLEncoder.encode("ichi", "UTF-8");
%>
<h4><a href="index.jsp?name=<%=name%>&id=<%=id%>">파라미터 인코딩해 넘기기</a></h4>
2) c:url로 sendRedirect 처리, c:param으로 파라미터도 같이 넘기기
<c:url var="paramIndex" value="/index.jsp">
<c:param name="name">이치</c:param>
<c:param name="id">ichi</c:param>
</c:url>
<h4><a href="${paramIndex}">c:url로 파라미터 넘기기</a></h4>
3) jsp:import와 jsp:include의 차이
import는 가져오기만 가능하지만,
include는 파라미터 넘기기도 가능
4) c:redirect와 jsp:forward의 차이
전자는 get방식, 후자는 post방식 차이
5) iframe으로 프레임 안에 홈페이지 내용 쳐넣을 수 있음(width, height 조절 가능)
<iframe src="http://jeongyeonstudio.com/8"></iframe>
<L03CoreUrl.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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.net.URLEncoder" %> <%@ 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>Core Url</title> </head> <body> <h1>Core 태그로 URL을 다루자</h1> <hr> <%String context = request.getContextPath();%> <h4><a href="<%=context%>/index.jsp">getContextPath()이용한 절대 경로</a></h4> <h4><a href="<c:url value="index.jsp"/>">c:url 태그 이용한 절대 경로(더 편리)</a></h4> <hr> <h4>c:url을 이용해서 parameter의 인코딩을 변경하자(URLEncoding)</h4> <h4><a href="index.jsp?name=이치&id=ichi">파라미터 넘기기</a></h4> <% String name = URLEncoder.encode("이치", "UTF-8"); String id = URLEncoder.encode("ichi", "UTF-8"); %> <h4><a href="index.jsp?name=<%=name%>&id=<%=id%>">파라미터 인코딩해 넘기기</a></h4> <!-- 퍼센트 인코딩: 보안 위해 문자를 퍼센트로 표현해줌. --> <c:url var="paramIndex" value="/index.jsp"> <c:param name="name">이치</c:param> <c:param name="id">ichi</c:param> </c:url> <h4><a href="${paramIndex}">c:url로 파라미터 넘기기</a></h4> <hr> <h1>c:import로 외부 페이지를 가져오자</h1> <%@ include file="./layout/header.jsp"%> <hr> <jsp:include page="./layout/header.jsp"> <jsp:param name="id" value="walker"></jsp:param> </jsp:include> <c:import url="./layout/header.jsp"/> <hr> <h4 align="center">외부 url을 가져오기</h4> <c:import url="http://www.yahoo.co.jp"/> <h4>iframe 태그 이용</h4> <iframe width="1000px" height="400px" src="http://jeongyeonstudio.com/8"></iframe> <h1>c:redirect(get 방식: parameter를 문자열로 보냄)</h1> <!-- context는 생략 가능 --> <%-- <c:redirect url="/index.jsp" context="/L13JSTL"/> --%> <h1>jsp:forward(post 방식: parameter를 request 내장 객체로 보냄)</h1> <jsp:forward page="/index.jsp"> <jsp:param name="id" value="walker"/> </jsp:forward> </body> </html> | cs |
<L04CoreException.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 | <%@ 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> <h1>Core 태그로 예외처리하자</h1> <c:catch var="exception"> <% String a = null; int b = Integer.parseInt(a); %> </c:catch> <h4>예외 발생(exception) : ${exception}</h4> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <c:catch var="numException"> <c:set var="num" value="abcd"/> <fmt:parseNumber type="number" value="${num}" var="parseNum"/> <h4>parseNum x 100 : ${parseNum*100}</h4> </c:catch> <h4>예외 발생 : ${numException}</h4> </body> </html> | cs |
'Programming > JSP' 카테고리의 다른 글
[MVC2 게시판] 2. 로그인 (0) | 2016.11.25 |
---|---|
[MVC2 게시판] 1. 개요 (0) | 2016.11.21 |
MVC 모델 (0) | 2016.11.07 |
[JSP] 가위바위보 게임 (0) | 2016.11.02 |
JSTL 태그 part1 (0) | 2016.10.30 |