Post

JSTL Quick Reference

JSTL Quick Reference

Expressions(EL)

접근방법

  1. Attribute 값 ${expression}을 직접 입력
  2. Bean bean.name 또는 bean["name"] 형식으로 빈의 이름과 프로퍼티 이름을 입력
  3. Map map.keyName 형식으로 맵의 키이름으로 입력

Map 타입의 글로벌 객체

객체이름 비고
param Request 객체에서 parameter에 대한 String 값
paramValues Request 객체에서 배열타입의 parameter에 대한 String 값
header Request 객체의 Header 정보 값

산술연산자

Operator(문자 연산자) Description
+ 더하기
빼기
* 곱하기
/ (div) 나누기(몫)
%(mod) 나누기(나머지)

관계 연산자

Operator(문자 연산자) Description
== (eq) 같음
!= (ne) 같지 않음
< (lt) 작음
> (gt)
<= (le) 같거나 작음
>= (ge) 같거나 큼

로직 연산자

Operator(문자 연산자) Description
&& (and) 모두 참일 경우
|| (or) 둘중에 하나만 참일 경우
! (not) 결과값의 반대
empty 객체가 null인 경우

Core 태그

데이타를 화면에 출력, Scope 변수의 생성 및 조작 그리고 Exception에 대한 처리 <%@ tablig prefix="c" uri="http://java.sun.com/jstl/core" %>

<c:out>

화면에 데이타 출력

1
<h2>Welcome, <c:out value="${user.name}" default="Guest"/></h2>
Attribute Description Req Default
value 출력될 데이타 YES None
default 데이타가 없을 경우 출력될 기본값 NO Body

<c:set>

Scope 변수에 데이타 저장

1
2
<c:set var="dogAge" value="${age div 7}"/>
You are <c:out value="${dogAge}"/> in dog years.
Attribute Description Req Default
var 저장된 데이타를 사용할 변수명 NO None
value 저장할 데이타 NO Body

<c:remove>

Scope 변수에 데이타 삭제

1
<c:remove var="dogAge" scope="page"/>
Attribute Description Req Default
var 삭제할 변수명 YES None
scope 스코프 변수 NO All scopes

조건 분기문

<c:if>

조건문에 의한 분기

1
2
3
<c:if test="${user.age ge 40}">
  You are over the hill.
</c:if>
Attribute Description Req Default
test 분기문에 대한 조건(true/false) YES None
var 분기문에 대한 조건결과 NO None

<c:choose>

switch문과 같은 다중 조건 분기문

1
2
3
4
5
6
7
8
9
10
11
12
13
<c:choose>
  <c:when test="${a boolean expr}">
    // do something
  </c:when>

  <c:when test="${another boolean expr}">
    // do something else
  </c:when>

  <c:otherwise>
    // do this when nothing else is ture
  </c:otherwise>
</c:choose>

<c:when>

switch문의 case와 같은 분기조건

<c:choose>와 함께 사용해야 한다.

Attribute Description Req Default
test 분기문에 대한 조건(true/false) YES None

<c:otherwise>

switch문의 default와 같은 분기조건 <c:choose>와 함께 사용해야 한다.

반복문

공통으로 사용되는 속성값

Attribute Description Req Default
begin 데이타의 시작점 NO 0
end 데이타의 종료점 NO 데이타의 길이만큼
step 데이타의 증가값 NO 1
varStatus 반복문 상에서 사용되는 속성값index데이타의 현재 index 값count데이타의 증가값(시작은 1부터)first데이타의 처음인지 여부last데이타의 마지막인지 여부 NO None

<c:forEach>

for문과 같은 데이타에 대한 반복문

1
2
3
4
5
6
7
<c:forEach items="${user.languages}" var="lang" varStatus="status">
  <c:if test="${status.first}">
    You speak these languages: <br><ul>
  </c:if>
  <li><c:out value="${lang}"/></li>
  <c:if test="${status.last}"></ul></c:if>
</c:forEach>
Attribute Description Req Default
var 반복문안에서 사용할 데이타 변수명 NO None
items 반복문안에서 사용할 데이타 No None

<c:forTokens>

문자열 데이타를 사용해서 for문과 같은 데이타에 대한 반복문

1
2
3
4
<c:set var="users">Fred,Joe,Mary</c:set>
<c:forTokens items="${users}" var="name" delims=",">
  <c:out value="${name}"/><br>
</c:forTokens>
Attribute Description Req Default
var 반복문안에서 사용할 데이타 변수명 NO None
items 반복문안에서 사용할 문자열 데이타 YES None
delims 문자열 데이타를 나눌 구분자 YES None

URL 관련

<c:import>

URL로 컨텐츠를 삽입하는 태그, <c:param>태그를 사용해서 쿼리 스트링을 넘겨줄 수 있다.

1
2
3
<c:import url="includes/header.jsp">
  <c:param name="title">Hello World<c:param>
</c:import>
Attribute Description Req Default
url 삽입할 컨텐츠 URL YES None
context 다른 로컬 웹 애플리케이션에서 삽입할 경우의 해당 애플리케이션 컨텍스트 NO 현재 컨텍스트

<c:url>

URL 규칙을 적용해서 작성할 때 사용(자동으로 컨텍스트 삽입 등…)

1
2
3
4
<c:url="editProfile.do" var="profileLnk">
  <c:param name="id" value="${user.id}"/>
</c:url>
<a href='<c:out value="${profileLnk}"/>'>
Attribute Description Req Default
url 삽입할 컨텐츠 URL YES None
context 다른 로컬 웹 애플리케이션에서 삽입할 경우의 해당 애플리케이션 컨텍스트 NO 현재 컨텍스트

<c:redirect>

페이지를 URL로 리다이렉트 함

1
2
3
<c:if test="${empty user}">
  <c:redirect url="login.do"/>
</c:if>
Attribute Description Req Default
url 리다이렉트할 URL YES None
context 다른 로컬 웹 애플리케이션으로 리다이렉트할 경우의 해당 애플리케이션 컨텍스트 NO 현재 컨텍스트

<c:param>

<c:import>, <c:url>, <c:redirect> 태그와 함께 사용해서 파라미터를 넘겨줌

Attribute Description Req Default
name 파라미터 이름 YES None
value 파라미터 값 NO Body

Function 태그

문자열을 조작하는 함수 <%@ taglib prefix="fn" uri="http://java.sun.com/jstl/functions" %>

1
${fn:function(arg0, …)}
Function Description
fn:contains(string, substring) substring 문자열이 string 문자열에 포함되었는지 확인
fn:containsIgnoreCase(string, substring) 대소문자 구분없이 substring 문자열이 string 문자열에 포함되었는지 확인
fn:join(string[], separator) string 배열을 separator를 사용해서 하나의 문자열로 변환
fn:length(collection or string) 콜렉션의 길이 또는 문자열의 길이
fn:replace(inputString, beforeString, afterString) inputString에서 beforeString을 afterString으로 변환
fn:split(string, delimiters) string 문자열을 delimiters로 나눠서 배열로 변환
fn:substring(string, beginIndex, endIndex) string으로 beginIndex부터 endIndex까지 자름
fn:substringAfter(string, substring) string 문자열 substring 이후부터 끝까지 자름
fn:substringBefore(string, substring) string 문자열 처음부터 substring 이전까지 자름
fn:trim(string) string 문자열의 whitespace를 모두 제거함
This post is licensed under CC BY 4.0 by the author.