JSP 표준 태그 라이브러리
■ JSTL은 라이브러리에 추가할 때 톰캣에 있는 라이브러리를 가져온다.
(jstl.jar, standard.jar가 필요)
■ JSTL은 JSP에서 스크립틀릿(자바코드)(<% %>)을 사용하지 않고
반복 작업(forEach var="" items="")을 하거나 조건문(<c:if test="${조건식}"> 조건식이 참 일때 수행</c:if>)을 실행할수 있는 태그를 제공
■ JSTL은 Core, XML, I18N, SQL, Function 5개의 태그라이브러리를 정의하고 있다.
■ JSTL의 Core 태그 라이브러리는 변수선언, 흐름제어, URL제어 등의 기능을 수행할 수있는
태그들을 정의
∎ <c:choose>, <c:when test="">, <c:othoerwise>
=> if ~ else if ~ else if ~ else 형태의 분기문 처리를 할 수 있다.
<c:if> 태그에는 else 처리를 할 수 있는 태그가 존재하지않는다.
※ <c:otherwise>는 필수 사항은 아님
[코드 따라하기] jstl.jsp
======================================================================
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="vo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
/*
JSTL이란 java standards tag libray의
준말로 표준 태크 라이브러리입니다.
el은 jsp에서 기본으로 사용가능하지만
jstl은 taglib등록해서 사용(지시어를 사용)
jstl에서 el문법 사용 가능
*/
<%
request.setAttribute("loginUser", new User("test", "1234", "테스트"));
//null일 때 확인할 것
// page = >request = > session => context 에 순차적으로 접근(scope)
//단순 if문 : <c:if></c:if>사용
//if else문 : <c:choose>, <c:when>, <c:otherwise>
//for문 : <c:forEach> (향상된 포문은 리스트 or 배열이 있으면 됨)
List<String> names = new ArrayList();
names.add("이용선");
names.add("김윤정");
names.add("정찬오");
names.add("오은혜");
//등록(액션에서 함)
request.setAttribute("names",names);
List<User> users = new ArrayList();
users.add(new User("test","1234","테스터"));
users.add(new User("test2","1234","테스터2"));
users.add(new User("test3","1234","테스터3"));
users.add(new User("test4","1234","테스터4"));
users.add(new User("test5","1234","테스터5"));
//등록(액션에서 함)
request.setAttribute("users", users);
%>
<!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>jstl연습</title>
</head>
<body>
<c:if test="${loginUser!=null}">
<h1>아이디 : ${loginUser.id }</h1>
<h1>닉네임 : ${loginUser.nickname }</h1>
</c:if>
<c:choose>
<c:when test="${loginUser!=null}">
<h1>로그인 되어있음</h1>
</c:when>
<c:otherwise>
<h1>로그인 안되어있음</h1>
</c:otherwise>
</c:choose>
<h2>우리반 사람들 이름 목록</h2>
<ul>
<c:forEach items="${names}" var="name">
<li>${name}</li>
</c:forEach>
<h2>유저목록</h2>
<table border="1">
<thead>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>닉네임</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.password}</td>
<td>${user.nickname}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>