2013. 7. 24. 14:08ㆍ프로그래밍/Java
JSTL 관련 정리가 일목요연하게 잘 되어있네요.
http://tazz009.tistory.com/484
설정
web.xml
<taglib>
    <taglib-uri>jstl-c</taglib-uri>
    <taglib-location>/WEB-INF/tlds/jstl/c.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>jstl-fmt</taglib-uri>
    <taglib-location>/WEB-INF/tlds/jstl/fmt.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>jstl-fn</taglib-uri>
    <taglib-location>/WEB-INF/tlds/jstl/fn.tld</taglib-location>
</taglib>
 
jsp 에서
<%@ taglib uri="jstl-c" prefix="c" %>
<%@ taglib uri="jstl-fmt" prefix="fmt" %>
<%@ taglib uri="jstl-fn" prefix="fn" %>
EL#
- 생존범위 속성 맵 - pageScope
- requestScope
- sessionScope
- applicationScope
 
- 요청 파라미터 맵 - param
- paramValues
 
- 요청 헤더 맵 - header
- headerValues
 
- 쿠키 맵 - cookie
 
- 컨텍스트 초기화 파라미터 맵(서블릿 초기화 파라미터 아님) - initParam
 
- 실제 pageContext 객체에 대한 참조. 이것은 빈임 - pageContext - pageContext 접근자 - getErrorData()
- getPage()
- getRequest()
- getResponse()
- getServletConfig)()
- getServletContext()
- getSession()
 
- JspContext로 부터 상속받은 접근자 - getAttribute()
- getAttributeNamesInScope()
- getAttributesScope()
- getExpression!Eval!uator()
- getOut()
- getVariableResolver()
 
 
 
스크립팅
- <$= request.getHeader("host") %>
EL 내장 객체
- ${header["host"]}
- ${header.host}
- ${headerValues.host[]}
스크립팅
- <%= request.getMethod() %>
EL 내장 객체
- ${pageContext.request.method}
core
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- 일반적인 것 - <c:out>
- <c:set>
- <c:remove>
- <c:catch>
 
- 조건 - <c:if>
- <c:choose>
- <c:when>
- <c:otherwise>
 
- URL 관련 - <c:import!>
- <c:url>
- <c:redirect>
- <c:param>
 
- 반복 - <c:forEach>
- <c:forEachToken>
 
① set
     - JSP의 setAttribute()와 같은 역활
     - 기본형
       <c:set   var="변수명"
                    value="변수명에 할당된 값"
                    target="자바빈 객체명이나 Map 객체명"
                    property="자바빈 객체나 Map 객체의 값을 설정할 프로퍼티 명"
                    scope="변수의 공유 범위(유효기간)으로 page|request|session|application" />
     - 예제
          <c:set var="country" value="${'Korea'}" />
          <c:set var="intArray" value="<%=new int[] {1,2,3,4,5}%>" />
          <c:set var="sum" value="${sum+i}" />
 
② out
     - JSP의 표현식을 대체하는 것으로 많이 사용됨
     - 기본형
          <c:out var="변수명"
                    default="기본값"
                    escapeXML="true|false" />
 
     * escapeXML
     > 생략시 기본값은 true
     > true일 경우 값 중에 포함된 <>&'" 문자들을 각각 <, >, &, ', "로 출력
     - 예제
          <c:out value="${sum}" />
          <c:out value="${val}" />
 
③ remove
     - JSP의 removeAttribute()와 같은 역활
     - 기본형
          <c:remove var="변수명"
                              scope="변수의 공유 범위로 page(생략 시 기본)|request|session|application" />
     - 예제
          <c:remove var="browser" />
 
④ catch
     - body 위치에서 실행되는 코드의 예외를 잡아내는 역할
     - 기본형
          <c:catch var="에러가 발생할 때 에러 메세지가 포함될 변수명" />
     - 예제
          <c:catch var="errmsg">
               line 1~
               <%=1/0%>
               line 2~
          </c:catch>
          <c:out value="${errmsg}" />
 
⑤ if
     - 조건문에 사용
     - 기본형
          <c:if   test="조건 판별식"
                    var="변수명"
                    scope="변수의 공유범위 page|request|session|application"
     - 예제
          <c:if test="${country != null}">
               국가명 : <c:out value="${country}" />
          </c:if>
 
⑥ choose
     - 자바의 switch문과 동일
     - 조건에서 문자열 비교가 가능
     - 하나 이상의 <when>과 하나의 <otherwise> 서브 태그를 가짐
 
⑦ when
     - choose 태그의 서브태그
     - choose 태그내에서 여러번 사용될 수 있다.
     - 기본형
          <c:when test="조건 판별식" />
 
⑧ otherwise
     - choose 태그의 서브태그
     - choose 태그내에서 한번만 사용될 수 있다.
     - 예제 :
          <c:choose>
               <c:when test="${country == 'Korea'}">
               나라 : <c:out value="${country}" />
          </c:when>
 
          <c:when test="${country == 'Canada'}">
               나라 : <c:out value="${country}" />
          </c:when>
   
          <c:otherwise>
               선택된 나라가 없습니다.
          </c:otherwise>
     </c:choose>
 
⑨ forEach
     - 객체 전체에 걸쳐 반복 실행할 때 사용
     - 기본형
          <c:forEach   items="반복할 객체명"
                              begin="시작값"
                              end="종료값"
                              step="증가값"
                              var="변수명"
                              varStatus="별도의 변수" />
⑩ forTokens
     - 문자열을 주어진 구분자(delimiter)로 분할
     - 기본형
          <c:forTokens items="반복할 객체명"
                              delims="구분자"
                              begin="반복할 시작값"
                              end="반목 마지막값"
                              step="증가값"
                              var="변수명"
                              varStatus="별도의 변수"
     - 예제
          <c:forTokens var="color" items="빨강색,주황색.노란색.초록색,파랑색,남색.보라색" delims=",.">
               color : <c:out value="${color}" /><br>
          </c:forTokens>
 
⑪ import!
 - 웹 어플리케이션 내부의 자원 및 http, ftp와 같은 외부에 있는 자원에 대해 접근
 - 기본형
  <c:import! url="읽어올 URL"
     var="읽어올 데이터를 저장할 변수명"
     scope="변수의 공유 범위"
     varReader="리소스의 내용을 Reader 객체로 읽어올 때 사용"
     charEncoding="읽어온 데이터의 캐릭터셋 지정" />
 
⑫ redirect
     - response.sendRedirect()를 대체하는 태그로 지정한 다른 페이지로 이동
     - 기본형
          <c:redirect url="이동할 URL" />
 
⑬ url
     - 쿼리 파라미터로 부터 URL 생성
     - 기본형
          <c:url var="생성한 URL이 저장될 변수명"
                    value="생성할 URL"
                    scope="변수의 공유 범위" />
 
⑭ param
     - 기본형
          <c:param name="파라미터 명"
                         value="파라미터 값" />
               <c:url var="registrationURL" value="/customers/register">
               <c:param name="name" value="${param.name}" />
               <c:param name="country" value="${param.country}" />
          </c:url>
<c:set var="temp" value="Hello! World" />
<c:out value="${ temp }" default="value is null"/><br>
<c:out value="${ temp }" default="value is null" escapeXml="false" /><br>
<c:out value="${ temp2 }" default="value is null" /><br>
<c:remove var="timezone" scope="session"/>
<c:set var="timezone" scope="session">CST</c:set>
<c:out value="${cookie['tzPref'].value}" default=="CST"/>
function
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
${fn:substring(name, 1, 10)}
fn:contains(string, substring)  
string이 substring을 포함하면 true 리턴. 
fn:containsIgnoreCase(string,substring)
대소문자에 관계없이, string이 substring을 포함하면 true 리턴. 
fn:endsWith(string, suffix)   
string이 suffix로 끝나면 true 리턴. 
fn:escapeXml(string)    
string에 XML과 HTML에서 특별한 의미를 가진 문자들이 있으면, XML 엔티티 코드로 바꿔준 뒤 문자열 리턴. 
fn:indexOf(string,substring)  
string에서 substring이 처음으로 나타나는 인덱스 리턴. 
fn:join(array, separator)  
array(배열) 요소들을 separator를 구분자로 하여 연결해서 리턴 
fn:length(item)      
item 이 배열이나 컬렉션이면 요소의 갯수를, 문자열이면 문자의 갯수를 리턴. 
fn:replace(string, before, after) 
string 내에 있는 before 문자열을 after 문자열로 모두 바꿔서 리턴. 
fn:split(string, separator)   
string 내의 문자열을 separator에 따라 나누어서 배열로 구성해 리턴. 
fn:startsWith(string, prefix)  
string이 prefix로 시작하면 true 리턴. 
fn:substring(string, begin, end) 
tring에서 begin 인덱스에서 시작해서 end 인덱스에 끝나는 부분
(end 인덱스에 있는문자 포함)의 문자열을 리턴.
fn:substringAfter(string, substring)
string에서 substring이 나타나는 이후의 부분에 있는 문자열을 리턴. 
fn:substringBefore(string, substring)
string에서 substring이 나타나기 이전의 부분에 있는 문자열을 리턴. 
fn:toLowerCase(string)    
string을 모두 소문자로 바꿔 리턴. 
fn:toUpperCase(string)    
string을 모두 대문자로 바꿔 리턴. 
fn:trim(string)      
string 앞뒤의 공백(whitespace)을 모두 제거하여 리턴.
fmt
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
- 국제화 - <fmt:message>
- <fmt:setLocale>
- <fmt:setTimeZone>
- <fmt:bundle>
- <fmt:setBundle>
- <fmt:param>
- <fmt:requestEncoding>
 
- 포맷팅 - <fmt:timeZone>
- <fmt:setTimeZone>
- <fmt:formatNumber>
- <fmt:formatDate>
- <fmt:parseNumber>
- <fmt:parseData>
- <fmt:parseNumber>
 
jstl fmt로 날짜보여줄때, pattern attribute에 의한 날짜 표현 방식들
pattern="yyyy-MM-dd aa h:mm:ss"    
2007-12-13 오전 9:36:48
pattern="yyyy-MM-dd aa hh:mm:ss"  
2007-12-13 오전 09:36:48
pattern="yyyy-MM-dd H:mm:ss"      
2007-12-13 9:36:48
pattern="yyyy-MM-dd HH:mm:ss"    
2007-12-13 09:36:48
<fmt:setLocale value="fr_CA" scope="session"/>
<fmt:setTimeZone value="Australia/Brisbane" scope="session"/>
<fmt:formatDate value="${blogEntry.created}" dateStyle="full"/>
<c:out value="${blogEntry.title}" escapeXml="false"/>
<fmt:formatDate value="${blogEntry.created}" pattern="h:mm a zz"/>
<fmt:formatNumber value="0.02" type="currency" currencySymbol="원"/>
<fmt:formatNumber value="9876543.61" type="number"/>
[type="{number|currency|percent}"]
<fmt:parseDate value="${usDateString}" parseLocale="en_US" type="both" dateStyle="short" timeStyle="short" var="usDate"/>
sql
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
- 데이터베이스 접근 - <sql:query>
- <sql:update>
- <sql:setDataSource>
- <sql:param>
- <sql:dataParam>
 
xml
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
- 코어 xml 액션 - <x:parse>
- <x:out>
- <x:set>
 
- xml 흐름 제어 - <x:if>
- <x:choose>
- <x:when>
- <x:otherwise>
- <x:forEach>
 
- 변환 액션 - <x:transform>
- <x:param>
 
'프로그래밍 > Java' 카테고리의 다른 글
| 이클립스 블럭 모드 (0) | 2014.12.01 | 
|---|---|
| URL 짧게 만들어주는 자바프로그램 만들기~ (0) | 2014.09.02 | 
| Nexus 4에서 안드로이드 개발자 옵션 켜기 (0) | 2013.05.14 | 
| Spring Framework 관련 강좌 또는 정보 사이트 (0) | 2012.09.13 | 
| 안드로이드 개발 강좌 - Tipssoft (0) | 2012.08.23 |